How do you do complex conditional code blocks in the new Web App framework?

I found one way to do conditional markup is to do things like

{{ `<p>here is my markup</p>` | endIf(false) }}

That’ll short circuit the output and NOT do the markup. However, in more advanced cases, I need to exclude huge blocks of code. And sometimes even nested.

if(a)
{
some stuff
  if(b)
  {
    some more stuff
  }
}

Short of creating an insane amount of partials to make life easier, or using server-side C# templates where I can do the complex conditions (just as “not ideal” as lots of partials) what options do I have?

You can use partials for large blocks of texts with the existing versions of ServiceStack Templates, but if you upgrade to v5.1.1 on MyGet you can use the new Handlebars like support for if blocks, see existing tests for examples on usage.

It follows the Handlebars block syntax which also contains some usage examples.

1 Like

That’s exactly what I was looking for. Thank you!

Maybe I spoke too soon. I’m on 5.1.1 and still getting errors.

Example in index.html:

{{#if forSelection }}
    Hello World!
{{/if}}

Exception:

ArgumentException: Invalid Syntax: expected start of identifier but was '#'
   at ServiceStack.Templates.JsTokenUtils.ParseIdentifier(StringSegment literal, JsToken& token)
   at ServiceStack.Templates.JsTokenUtils.ParseJsToken(StringSegment literal, JsToken& token, Boolean filterExpression)
   at ServiceStack.Templates.JsExpressionUtils.ParseJsExpression(StringSegment literal, JsToken& token, Boolean filterExpression)
   at ServiceStack.Templates.TemplatePageUtils.ParseTemplatePage(StringSegment text)
   at ServiceStack.Templates.TemplatePage.Load()
   at ServiceStack.Templates.TemplatePage.Init()
   at ServiceStack.Templates.PageResult.Init()
   at ServiceStack.Templates.PageResult.WriteToAsyncInternal(Stream outputStream, CancellationToken token)
   at ServiceStack.Templates.PageResult.WriteToAsync(Stream responseStream, CancellationToken token)
   at ServiceStack.TemplatePageHandler.ProcessRequestAsync(IRequest httpReq, IResponse httpRes, String operationName)

I’m using SS 5.1.1 and .NET Core 2.1.

You have an old version of v5.1.1, you need to clear your NuGet cache to download the latest version:

$ nuget locals all -clear

https://templates.servicestack.net is a .NET Core App that has been with the latest version which you can check out by pasting in:

{{ true | assignTo: forSelection }}
{{#if forSelection }}
    Hello World!
{{/if}}

In any of the live text editors like the textarea on the home page.

That was it. All take care of now, thank you!