[stringtemplate-interest] accept simple expression language (DVD)

Volkan Ceylan volkanceylan at gmail.com
Mon May 11 23:20:28 PDT 2009


Though it violates ST principles, it's possible, and i implemented it in my
.NET project.

You can either use a a combination of expression evaluator + renderer , or a
fake object implementing IDictionary interface.

Let's say we have a dummy class ExpressionProvider.

st.SetAttribute("_expression", new ExpressionProvider())
st.RegisterAttributeRenderer(typeof(ExpressionProvider),
MyExpressionRenderer)

MyExpressionRenderer is a class that implements IAttributeRenderer
interface.

Now in template we write

$_expression;format=$O.a$+"+"+$O.b$$

If O.a is 5 and O.b is 4, when format is turned into a string, format will
be evaluated as "5+4". When your
MyExpressionRender.ToString() method called with this string, just call your
evaluator library and return result as a string.

Other option is to use a fake object implementing IDictionary interface.
Let's say this object is also called _expression.

Now in template, we write
_expression.(O.a + "+" + O.b)

ST first evaluates expression in paranthesis and looks up "5+4" in
_expressio object as if it was a property. But because _expression
implements IDictionary interface, ST calls get value method of _expression
object.
Now you can handle it as a expression and use your favorite evaluation
library too.

As i said, though its possible and kind of a hack, its against ST's
principles, and i try to avoid it by doing calculations in the model itself
or in generated javascript where available. Use such techniques at your own
risk when no other option is available.

>
> $O.a$$O.b$
>
> but a user wants to have only value  c = summing a and b,  something like
> $O.a+O.b$
>
> I guess in order to get this, model dev has to add property "c" in  O in
> order for author
> to get.   This is a big problem if an application expects authors to
> import their own templates,
> since the authors would have no way to customize it, even for some
> simple operation.
> Renderer might not help so I am not sure if there is any solution for
> this scenario. I went through
> the doc but did not have an example.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20090512/92dccafa/attachment.html 


More information about the stringtemplate-interest mailing list