How to cancel anonymous list in a set of templates?

Is it possible to cancel an anonymous list in a template set?

eg.

[% FOREACH month IN [1..12].reverse %] [% month %] [% END %] 

(except that does not work).

I just wondered if there was a neat way to do this without using variables or explicitly printing the array.

+6
perl template-toolkit
source share
3 answers

Sorry, but no. The ability to create anonymous arrays in situ is a special case that the TT parser handles. You cannot work with them, as in normal Perl, without the intermediate step of assigning a named variable.

EDIT: You cannot even execute a routine to try to use it like this:

 [% FOREACH month IN my_reverse([1..12]) %] [% month %] [% END %] 

Also you cannot use a method for an object:

 [% FOREACH month IN testObj.my_reverse([1..12]) %] [% month %] [% END %] 

They will compile, but in each case the FOREACH construct sees the head of the chain, for example. a CODE in the first case and a blissful object in the second.

+10
source share

I'm a little newbie, but what about this:

[% months = ['jan', 'feb', 'mar', ...]; ## months array

[% FOREACH i = [(months.size-1) -1 .. 0]%] ## counts -11 to 0 for 12 elements [% months [(i-1)] ## converts I to a positive number - displays months [11], months [10] ... months [0]

0
source share
 <!-- show 2010 2009 2008 2007 --> [% SET startyear = 2007 %] [% SET endyear = 2010 %] [% SET allyears = [ startyear .. endyear ] %] [% FOREACH year IN alleyears.reverse %] <li>[% year %]</li> [% END %] 
0
source share

All Articles