Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template::Toolkit don't evaluate variable as string

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?

like image 265
srchulo Avatar asked Jul 10 '14 04:07

srchulo


1 Answers

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.

like image 194
stevenl Avatar answered Oct 01 '22 07:10

stevenl