Templates: parentheses in expressions

I have written a following piece of code:

{{ 'xx' | assignTo: x }} {{ ('<h3>' + x + '</h3>') | raw | ifNotEmpty(x) }}

this does work when tested in the template evaluat on the http://templates.servicestack.net/docs/default-filters

but does not work when I’m trying it in my code (the unhandled expression is called).

using System;
using ServiceStack.Templates;
using System.Collections.Generic;

var context = new ServiceStack.Templates.TemplateContext(){
 OnUnhandledExpression = f =>
                {
                    throw new Exception("unknown binding " + f.BindingString);
                    return new byte[0];
                }          
}.Init();

var output = context.EvaluateTemplate("{{ 'xx' | assignTo: x }} {{  ('<h3>' + x + '</h3>') | raw | ifNotEmpty(x) }} ");

what is wrong? why does it behave different (properly, IMO) in the templates docs?

This is the new support added in v5.1.1 on MyGet. Basically ServiceStack Templates internals was rewritten to utilize a proper JS AST which now supports most JavaScript expressions (i.e. not statements or assignment expressions).

I see, thank you for the response.
Unfortunatelly I’m stuck with the previous version for now. How can I achieve such condition test within the old version?

The old version doesn’t support evaluating expressions so you need to use filters for any functionality, the string functions contains different examples of appending strings:

http://templates.servicestack.net/docs/default-filters#string-functions

1 Like