Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to compare two variables in Template Toolkit?

[% IF OrgType.id == Organization.org_type_id %]selected="selected"[% END %] 

Does not work even when they both evaluate to the same number.

[% IF OrgType.id == 3 %]selected="selected"[% END %] 

(i.e. hard-coding in a number for testing purposes) does work.

[% OrgType.id %] and [% Organization.org_type_id %] 

both print "3" on the page.

like image 687
Devin Ceartas Avatar asked Feb 12 '10 02:02

Devin Ceartas


1 Answers

The following works for me:

 my $tt = Template->new; 
 $tt->process( \"[% IF foo == bar %]blah[% END %]", { foo => 42, bar => 42 } );

That outputs 'blah'. So I suspect that your two variables don't contain what you think they do. Template Toolkit uses string equality for ==, so if you do:

 my $tt = Template->new; 
 $tt->process( \"[% IF foo == bar %]blah[% END %]", { foo => 42, bar => "42 " } );

It will break. You may need to massage the data a bit to get them to work right with string equality.

like image 95
friedo Avatar answered Oct 15 '22 21:10

friedo