使用Template Toolkit从列表中过滤项目

时间:2011-08-11 18:52:15

标签: perl template-toolkit

如何过滤Template Toolkit中动态生成列表中的某些项目? 我有一个id列表(也是动态生成的)和一个要排除的id列表,我需要只获取未排除的ID。最好的方法是什么? 示例代码:

[% SET ids = [1,2,4,10,11,12,13,17,19,20,21,50,51] %]
[% SET id_excluded = [10,11,13,20,50] %]
[% FOREACH pid IN ids %]
    [% IF ?code to filter the ids? %]
        [% pid %]
    [% END %]
[% END %]

1 个答案:

答案 0 :(得分:4)

尝试grep VMethod列表,例如:

[% SET ids = [1,2,4,10,11,12,13,17,19,20,21,50,51] %]
[% SET id_excluded = [10,11,13,20,50] %]
[% FOREACH pid IN ids %]
    [% UNLESS id_excluded.grep("^$pid\$").size %]
        [% pid %]
    [% END %]
[% END %]

产生以下内容:

1 2 4 12 17 19 21 51