【问题标题】:Is there a way to compare two variables in Template Toolkit?有没有办法比较模板工具包中的两个变量?
【发布时间】:2011-01-16 00:05:43
【问题描述】:
[% IF OrgType.id == Organization.org_type_id %]selected="selected"[% END %] 

即使它们都计算为相同的数字也不起作用。

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

(即为了测试目的在数字中硬编码)确实有效。

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

两者都在页面上打印“3”。

【问题讨论】:

  • 在 Perl 方面,确保它们完全一样。

标签: perl template-toolkit


【解决方案1】:

以下对我有用:

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

输出'blah'。所以我怀疑你的两个变量不包含你认为他们所做的事情。模板工具包对== 使用字符串相等,所以如果你这样做:

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

它会坏掉的。您可能需要稍微修改数据以使它们在字符串相等的情况下正常工作。

【讨论】:

    猜你喜欢
    • 2014-11-07
    • 1970-01-01
    • 2012-09-19
    • 2017-11-27
    • 2020-05-06
    • 2020-03-13
    • 2013-06-15
    • 1970-01-01
    • 2013-05-19
    相关资源
    最近更新 更多