Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reverse an anonymous list in template toolkit?

Is it possible to reverse an anonymous list in template toolkit?

e.g.

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

(except that doesn't work).

Just wondered if there was a neat way to do it without using variables or explicitly typing out the array.

like image 700
aidan Avatar asked Jan 15 '10 16:01

aidan


1 Answers

Sorry, there isn't. Being able to instantiate anonymous arrays in situ is a special case handled by the TT parser. You can't operate on them like you can in regular Perl without the intermediate step of assigning to a named variable.

EDIT: You can't even pass in a subroutine to try to use like so:

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

Nor can you try to use a method on 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, e.g. a CODE reference in the first case and a blessed object in the second.

like image 135
Adam Bellaire Avatar answered Sep 28 '22 05:09

Adam Bellaire