Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter items from a list using Template Toolkit

How can I filter some items that are in a dynamically generated list in Template Toolkit? I have a list of ids (also dynamically generated) and a list of ids to be excluded and I need to get just the ids that are not excluded. What the best way to do that?? Sample code:

[% 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 %]
like image 298
nsbm Avatar asked Nov 17 '25 09:11

nsbm


1 Answers

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
like image 56
RET Avatar answered Nov 20 '25 10:11

RET



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!