Template talk:Foreach
Add topicThis template has stopped working due to some software change. Use Template:For instead.
containing
{{foreach/pass1 |v@= |c={{{call}}} |pv={{{pv|1}}} |s={{{separator|}}}{{{sep|}}} |pc1={{{pc1|=}}} |pc2={{{pc2|=}}} |pc3={{{pc3|=}}} |pc4={{{pc4|=}}}| {{{1|@}}}|{{{2|@}}}|{{{3|@}}}|{{{4|@}}}|...|{{{150|@}}}}}
using Template:Foreach/pass1
containing
<!-- -->{{{{{{{{v{{{1|}}}|c}}}|x0}}}|{{{pv}}}={{{1}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}}}{{{v{{{2|}}}|{{{s|}}}}}}<!-- -->{{{{{{{{v{{{2|}}}|c}}}|x0}}}|{{{pv}}}={{{2}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}}}{{{v{{{3|}}}|{{{s|}}}}}}<!-- -->{{{{{{{{v{{{3|}}}|c}}}|x0}}}|{{{pv}}}={{{3}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}}}{{{v{{{4|}}}|{{{s|}}}}}}<!-- ... -->{{{{{{{{v{{{150|}}}|c}}}|x0}}}|{{{pv}}}={{{150}}}|{{{pc1}}}|{{{pc2}}}|{{{pc3}}}|{{{pc4}}}}}
Documentation
[edit]Description
[edit]The template calls a specified template up to 150 times, where one parameter takes each of a list of values; in addition to the variable parameter, for up to 4 parameters a fixed value can be specified.
Usage
[edit]{{foreach |call=template to call |sep=what to separate the entries with |pc1=parameter name=constant value |pc2=parameter name=constant value |pc3=parameter name=constant value |pc4=parameter name=constant value |pv=name of variable parameter |list of up to 150 parameter values, with separator "|" }}
Examples
[edit]Code | Result |
---|---|
1{{foreach |00|01|02|03|04|05|06|07|08|09 |10|11|12|13|14|15|16|17|18|19 |20|21|22|23|24|25|26|27|28|29 |30|31|32|33|34|35|36|37|38|39 |40|41|42|43|44|45|46|47|48|49 |50|51|52|53|54|55|56|57|58|59 |60|61|62|63|64|65|66|67|68|69 |70|71|72|73|74|75|76|77|78|79 |80|81|82|83|84|85|86|87|88|89 |90|91|92|93|94|95|96|97|98|99 |call=x1 |sep=, 1 }} |
1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1=, 1= |
Note that the last part of the separator is used as a prefix; the first prefix needs to be put separately.
{{foreach|a|3||c|call=x3|sep=-}} using Template:X3 gives
- ===-===-===-===
{{foreach|p|||3||c|call=t|sep=///}} using Template:T gives
- start-=-end///start-=-end///start-=-end///start-=-end///start-=-end///start-=-end
{{foreach|call=short DOW|3|1|5|3|2|sep= / }} using Template:Short DOW gives
- = is not a valid weekday number. / = is not a valid weekday number. / = is not a valid weekday number. / = is not a valid weekday number. / = is not a valid weekday number.
{{foreach|call=t2|pc1=2=constant|abc|def|ghi}} using Template:T2 gives:
Parameter 1 is "2=constant", parameter 2 is "="Parameter 1 is "2=constant", parameter 2 is "="Parameter 1 is "2=constant", parameter 2 is "="
A constant parameter overrides the variable parameter if it has the same name as the implicitly or explicitly given name of the latter:
{{foreach|call=t2|pc1=1=7|pc2=2=5|abc|def|ghi|sep=,}} using Template:T2 gives:
Parameter 1 is "1=7", parameter 2 is "2=5",Parameter 1 is "1=7", parameter 2 is "2=5",Parameter 1 is "1=7", parameter 2 is "2=5"
This is a difference with Template:For.
Semi-recursive: see Template talk:Foreach/recursion
A reserved name that cannot be used as list item is "@"; it is ignored, but e.g. @1 is not:
{{foreach|a|@|@1|b|c|call=x3|sep=-}} gives
- ===-===-===-===
Related template
[edit]This template is based on w:en:Template:Foreach. A difference is:
- The Meta version handles empty parameter values in the regular way, while the en version ignores them.
See also
[edit]Discussion
[edit]I don't know what you are trying to do, but the version I did, can handle "@" as a list item AzaToth 04:23, 4 December 2005 (UTC)
- This version handles empty parameter values in the regular way, that may be useful in some applications. The @ was chosen as a symbol not needed as parameter value. Another special character or sequence could also be chosen.--Patrick 10:01, 4 December 2005 (UTC)
Why I didn't added named extra parameters to my version, is because you could easly type with extra call parameter 1=foo=bar, also, could you explain the first difference?
I also think my fist verion could hav problem if calling some templates, fixes as:
{{{{{c{{{c{{{v001|}}}|}}}|void}}} |{{{p0}}}={{{v001}}} |{{{p1}}} |{{{p2}}} |{{{p3}}} |{{{p4}}} }}
instead of
{{{v001{{{v001|}}}|{{{{{c}}} |{{{p0}}}={{{v001}}} |{{{p1}}} |{{{p2}}} |{{{p3}}} |{{{p4}}} }}}}}
(calling void instead of template if v001... is nada)
AzaToth 13:17, 4 December 2005 (UTC)
OK, I understand now what you mean by ignoring empty parameters, I was ignoring that case because I was didn't think anyone would apply empty parameters in the arguments AzaToth 13:45, 4 December 2005 (UTC)
Changed pass1
[edit]Changed like I did in the en version so the called template always is outside parameter AzaToth 17:57, 10 December 2005 (UTC)
- What is the advantage? It seems a little more complicated.--Patrick 00:25, 11 December 2005 (UTC)
- The older version can have problem if the called template returns a part of a template for example (a row) AzaToth 00:43, 11 December 2005 (UTC)
- Can you give an example?--Patrick 10:05, 11 December 2005 (UTC)
- Can try:
Template:t4 containing "
|-
|Data
|{{{1}}}
"
Data | = |
Data | = |
Data | = |
Data | BA |
Data | C |
Ok, now I'm confused, when I typed the above, the for didn't work, now it works :/ AzaToth 12:12, 11 December 2005 (UTC)