[stringtemplate-interest] [attr:tpl];separator

Roman Odaisky roma at qwertty.com
Thu Jul 31 14:08:52 PDT 2008


Greetings,

Having gotten StringTemplate to work with Django, I wanted to make a list of 
certain data.

If I write something like

$[obj.color, obj.size, obj.volume]; separator=" | "$

I get

Yellow | Large | 42

as intended. But then I wanted to modify the presentation of one of the 
attributes, namely, to append the unit to the number. I tried using 
obj.volume:{$it$ m³} and that didn’t work, neither did anything else I 
attempted.

What is the right way to include the result of template application to a list?

Or, say, I want to achieve the same thing this template does:

A=$a$, B=$b$, C=$c$, D=$d$

except if any of a, b, c or d attributes are null, their entries must not be 
shown, like this:

A=1, B=2, D=4

Is there a way to construct a list from constant strings in the template? If 
there were such a list, I would have written

$["A", "B", "C", "D"],[a, b, c, d]:{ k, v | $k$=$v$}; separator=", "$

Indeed I’ve just checked after having written this, and this exact notation 
worked, much to my surprise as it doesn’t seem to be documented anywhere. 
Though it doesn’t solve the problem completely: if an attribute from the 
second list is null, the corresponding item from the first one isn’t removed.

So, my problem is:
1. To take a list of attributes, some of which can be null;
2. To apply certain templates to some of the non-null ones;
3. To join the result using a separator.

A partial solution found while typing this message is:

$[{A=$a$}, {B=$b$}, {C=$c$}, {D=$d$}]; separator=", "$

Though these subtemplates always produce nonnull results which cause extra 
separators to appear if some attributes are empty.

Is there a way to solve the problem with StringTemplate?

Also, is the documentation lacking with regard to the above or am I too 
inattentive?

-- 
TIA
Roman.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2286 bytes
Desc: not available
Url : http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20080801/7d350b92/attachment.bin 


More information about the stringtemplate-interest mailing list