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 %]
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With