I'm using Template::Toolkit and within a for loop I am trying to change the timezone of an object:
[%- FOR item IN c.user.items -%]
[% item.date.set_time_zone(c.user.timezone.name) %]
Date: [% item.date %] <br />
[% END %]
It works and it changes the timezone, but the set_time_zone
function returns the DateTime object which is then evaluated onto the page. So every time around the for loop the string is evaluated and put onto the page. I know that some other templating systems have two sets of tags, one that says just run this code and another that says evaluate this and put it on the page. Does Template::Toolkit have anything like this? I've looked but I can't seem to find anything. The only way I've found so far to not have the string evaluated on the page is set the value that is returned to a variable:
[% var = item.date.set_time_zone(c.user.timezone.name) %]
Does anyone know of a better way to do this in Template::Toolkit?
Try the CALL
directive:
[% CALL item.date.set_time_zone(c.user.timezone.name) %]
From the documentation:
The CALL directive is similar to GET in evaluating the variable named, but doesn't print the result returned. This can be useful when a variable is bound to a sub-routine or object method which you want to call but aren't interested in the value returned.
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