Sometime I need template result like:
[1,2,3,4]
{
'a',
'b',
'c'
}
(there is no more comma after the last element)
Normally, we use template like below:
{% for item in items %}
{{ escape(item) }},
{% end %}
It's not worked.
I know str.join function work fine in these situations. My question is:
Is there any solution in Template?
google-ctemplate solution:
http://ctemplate.googlecode.com/svn/trunk/doc/reference.html#separator
Thanks
-Ken
These are not template solution.
Sometimes there are too much code for process looped item, str.join
maybe is not the best solution.
Please take a look google-ctemplate separator[1]:
{{#ATTENDEES}}
{{NAME}}
{{#ATTENDEES_separator}}, {{/ATTENDEES_separator}}
{{/ATTENDEES}}
I wrote huge mount of templates in ctemplate format, the separator is
very useful.
1: http://ctemplate.googlecode.com/svn/trunk/doc/reference.html#separator
-Ken
{% for i, attendee in enumerate(attendees) %}
{{ attendee.name }}{% if i != len(attendees) %}, {% end %}
{% end %}
You could also write an enumerate-like ui_method that would let you
test whether you're on the last element of a list in a less clunky
way.
-Ben
Thanks Ben
Thanks to everyone