如何使用模板工具包检查数组中是否存在元素

how to check whether an element is present in an array using template toolkit

我正在尝试找出数组是否包含模板工具包中的元素。不确定如何?有人可以帮忙吗?

my $var ={
myarray => ['a','c','b','d']
}

传递给 tt:

现在要检查:

[% IF ( myarray contains 'a') %]
[% END %]

[% IF ( myarray contains 'e') %]
[% END %]

grep

Returns a list of the items in the list that match a regular expression pattern.

因此,

[% IF myarray.grep('^e\z').size %]
<p>Hello World</p>
[% END %]
myarray = ['17', '177', '166']
value = 16

[% IF myarray.grep('^$value$').size %] # will be true
<p>Hello World</p>
[% END %]

myarray.grep('^$value$') # return ['166']

另一种方式

[% IF myarray.grep('^$value$').0 == value %] # will be false
<p>Hello World</p>
[% END %]