Template:Foreach: Difference between revisions
(bring improvements over from Foreach2) |
(rewrote docs for rewritten Foreach template; /cat) |
||
Line 13: | Line 13: | ||
-->{{#ifexpr:{{{up_to}}}>11|{{{{{call}}}|{{#explode:{{{item_in}}}|{{{delim|,}}}|12}}|{{{pass_thru_name1}}}={{{pass_thru_arg1|}}}|{{{pass_thru_name2}}}={{{pass_thru_arg2|}}}}}}}<!-- | -->{{#ifexpr:{{{up_to}}}>11|{{{{{call}}}|{{#explode:{{{item_in}}}|{{{delim|,}}}|12}}|{{{pass_thru_name1}}}={{{pass_thru_arg1|}}}|{{{pass_thru_name2}}}={{{pass_thru_arg2|}}}}}}}<!-- | ||
-->{{#ifexpr:{{{up_to}}}>12|{{{{{call}}}|{{#explode:{{{item_in}}}|{{{delim|,}}}|13}}|{{{pass_thru_name1}}}={{{pass_thru_arg1|}}}|{{{pass_thru_name2}}}={{{pass_thru_arg2|}}}}}}}</includeonly><noinclude> | -->{{#ifexpr:{{{up_to}}}>12|{{{{{call}}}|{{#explode:{{{item_in}}}|{{{delim|,}}}|13}}|{{{pass_thru_name1}}}={{{pass_thru_arg1|}}}|{{{pass_thru_name2}}}={{{pass_thru_arg2|}}}}}}}</includeonly><noinclude> | ||
Heavily adapted from a Wikipedia template of the same name, '''Foreach''' parses the text given by parameter "item_in", turning it into a list of items separated by a delimiter (by default the comma, or specified by the "delim" parameter), then passes each item to a template named in the "call" parameter. You must specify how many items from the list to send to the template, as there is no easy way to count the items automatically. | |||
[[Category: | '''Foreach''' also accepts two pass-through arguments, that is, two arguments to send through to the "call" template in addition to each item from the list being passed to that template as "<nowiki>{{{1}}}</nowiki>"; these arguments are named with "pass_thru_name1" and "pass_thru_name2" and their values are given with "pass_thru_arg1" and "pass_thru_arg2". | ||
In other words, given some text that contains a list like "A,B,C,D", and the name of a template that can process those items as well as two named parameters, this template can generate calls to a template like: <nowiki>{{Template|A|format=table|color=blue}}</nowiki>, <nowiki>{{Template|B|format=table|color=blue}}</nowiki>, etc. One quirk of this template is that the first item in the list is ignored; the complex reason for this is due to the way it is used as part of the BSL documentation, and is explained on [[BSL:Grouping]]. | |||
[[Category:Utility templates]] | |||
</noinclude> | </noinclude> |
Revision as of 04:09, 13 December 2015
Heavily adapted from a Wikipedia template of the same name, Foreach parses the text given by parameter "item_in", turning it into a list of items separated by a delimiter (by default the comma, or specified by the "delim" parameter), then passes each item to a template named in the "call" parameter. You must specify how many items from the list to send to the template, as there is no easy way to count the items automatically.
Foreach also accepts two pass-through arguments, that is, two arguments to send through to the "call" template in addition to each item from the list being passed to that template as "{{{1}}}"; these arguments are named with "pass_thru_name1" and "pass_thru_name2" and their values are given with "pass_thru_arg1" and "pass_thru_arg2".
In other words, given some text that contains a list like "A,B,C,D", and the name of a template that can process those items as well as two named parameters, this template can generate calls to a template like: {{Template|A|format=table|color=blue}}, {{Template|B|format=table|color=blue}}, etc. One quirk of this template is that the first item in the list is ignored; the complex reason for this is due to the way it is used as part of the BSL documentation, and is explained on BSL:Grouping.