3

如何过滤 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 %]
4

1 回答 1

4

Try the grep VMethod for lists, eg:

[% 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 %]

Which produces the following:

1 2 4 12 17 19 21 51
于 2011-08-12T06:57:05.043 回答