[stringtemplate-interest] accept simple expression language (DVD)
DVD
dvd at newfoundmarket.com
Tue May 12 03:28:27 PDT 2009
Thanks. The second approach is quite close to what is in my mind as it
presents the end user
with an easy to grasp syntax. It would be nicer to only need to use
_expression_add(O.a , O.b)
which would be even easier for user to use. (_expression is a special
label tells ST engine this is "add" is
an predefined method of (built-in or extended) expression object that would
take the values of O.a and O.b and return the computed value) . This
syntax resembles
a conventional function call syntax.
Yes, I'd probably stick to ST principles 99% of my projects
but this capability would probably enable me to use ST for 100% of my
project
without show stoppers.
Hope some kind of formal syntax/approach like this could be part of the
ST core function
and have a built-in expression object for common purposes.
regards.
Volkan Ceylan wrote:
> 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.
> ------------------------------------------------------------------------
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>
More information about the stringtemplate-interest
mailing list