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

Roman Odaisky roma at qwertty.com
Fri Aug 1 02:07:55 PDT 2008


On Friday, 01.08.2008 01:35:58 Terence Parr wrote:
> On Jul 31, 2008, at 2:08 PM, Roman Odaisky wrote:
> > 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.
>
> ST might not allow arbitrary expressions within the list constructor
> [...], although that doesn't sound like something I would miss. Did
> you try obj.volume:{x | $x$ units} ?

The template $[a:{$it$}]$ throws antlr.RecognitionException, which then 
happens to be obscured by this:
  
File "/usr/lib/python2.5/site-packages/stringtemplate3/language/ActionParser.py", 
line 887, in list_
    self.reportError(ex)
  
File "/usr/lib/python2.5/site-packages/stringtemplate3/language/ActionParser.py", 
line 75, in reportError
    if group == stringtemplate3.StringTemplate.defaultGroup:
AttributeError: type object 'StringTemplate' has no attribute 'defaultGroup'

(a bug?). $[a:{x|$x$}]$ doesn’t work as well.

> > 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
>
> You'd have to put $if(a)$A=$a$$endif$ and so on.

Do I understand correctly that $if(an_undefined_attribute)$ something $endif$ 
is either “something”, or an empty string, but never a null value? And if ST 
encounters such a value in a list, it doesn’t omit it?

> > 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.
>
> Right.  ST assumes that the parallel lists have the same size.
>
> You could make or pass in a map that mapped iteration to units:
>
> units ::= [
> 	"1" : "A",
> 	"2" : "B"
> 	...
> ]
>
> and then do units.(i) inside an iteration where i is an implicitly
> maintained loop iteration variable. Naturally, the solution is icky.
>
> Try:
>
> $["A", "B", "C", "D"],[a, b, c, d]:{ k, v | $if(v)$$k$=$v$$endif$};
> separator=", "$

>>> st = StringTemplate('$["A", "B", "C", "D"],[a, b, c, d]:{ k, v | 
$if(v)$$k$=$v$$endif$}; separator=", "$')
>>> st['a'] = 'aa'
>>> st['b'] = 'bb'
>>> st['d'] = 'dd'
>>> print unicode(st)
A=aa, B=bb, C=dd,

My suggestion would be to make sure lists allow calling templates like this:

$[a, b:someTemplate(), c]$
or even
$[a, [b1, b2]:someTemplate(), c]$

And if b were missing (if it would fail the $if(b)$ test), then the template 
wouldn’t be applied to it at all, and the list would contain one less item. 
(Those who need it to be applied even if the attribute is empty, will simply 
use $[a, {$b$}, c]$.) Is it feasible?

I’m currently developing a Web site and I have to display some text in a line 
with separators between groups. But I guess such a feature as I suggested 
above would be useful elsewhere. For example, in generating Python code with 
named arguments:

call ::= "some_function(<[a:{aa=<a>}, b:{bb=<b>}, c:{cc=<c>}, d:{dd=<d>}]; 
separator= ", ">)"

call(a=1, b=2, d=4) <! some_function(aa=1, bb=2, dd=4) !>

-- 
WBR
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/309c1901/attachment-0001.bin 


More information about the stringtemplate-interest mailing list