[stringtemplate-interest] List-making problem

Mikael Nyberg mikaeln at acm.org
Mon Dec 1 03:19:38 PST 2008


>
> There has been some discussion about replacements for $if$ in the thread
> starting from <200808101759.19093.roma at qwertty.com>.

Nice, makes a lot of sense.

> In short, you can replace $if(condition)$template$endif$ with $condition:
> {template}$. If  condition can be a list, use $first(condition):{template}$
> to avoid expanding the template more than once. Instead of elseif you can use
> the null=... clause.

Well, the replacement above works fine, but it is not always
exchangeable in the context of lists when using templates in groups.
At the top level (in the "main", no arguments) template in the group
file I'm allowed to test for existence of any attribute: The test
$if(attr)$...$endif$ is legal even if attr does not exist. But if
write the condition as $attr:{template}$ and put it in a list
$[{$attr:{template}$}]$ I may get a "no such attribute: attr in
template context" error. I admit I have not really understood this
part of StringTemplate; i.e. when it is legal to test for attributes
that may not have been set (or why it should ever be illegal). Maybe
what happens in the list construction when using braces to embed an
expression is the same as "calling" a subtemplate with an argument?

> Also you can use any expressions in [] lists, for example, [attr, "literal",
> {template $something$}, "argument":template()]. This might solve your problem
> if I understand it correctly:
>
> $[a, b, c, {$list; separator=", "$}, x, y, z]; separator="; "$

Yes, I see now that if I embed the expression i braces, any
expressions can be used. That leaves me with the problem described
above with non-existing attributes.

> Or you could maybe define a standalone template like this:
>
> (separated.st)
> $it; separator=s$
>
> and then invoke it as
>
> $[a, b, c, list:separated(s=", "), x, y, z]; separator="; "$
>
> (doesn't seem to work, however :-).

No this is what I tried first, seems that the expression must be put
within braces.
-- 
Mikael Nyberg
mikaeln at acm.org
tel: 91830360


More information about the stringtemplate-interest mailing list