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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With