Difference between revisions of "Foreach"

From Sega Retro

(I didn't realise I had got this working)
(correction: it doesn't work at all)
Line 1: Line 1:
<div style="display:none;">{{#vardefine:tempList|{{{list}}}}}
+
<div style="display:none;">{{#vardefine:i|0}}
{{#vardefine:delimiter|{{{delimiter}}}}}
 
{{#vardefine:i|0}}
 
 
{{#vardefine:currentValue|NULL}}
 
{{#vardefine:currentValue|NULL}}
 
</div>{{#while:
 
</div>{{#while:
| {{#ifexpr:{{#len:{{#var:currentValue}}}} >= {{#len:{{{delimiter}}}}}|true|}}
+
| {{#if:{{#explode:{{{list|}}}|{{{delimiter}}}|{{#var:i}}}}|true|}}
|{{#vardefine:currentValue|{{#explode:{{#var:tempList}}|{{#var:delimiter}}|{{#var:i}}}}}}{{incvar|i}}
+
|{{#vardefine:currentValue|{{#explode:{{{list|}}}|{{{delimiter}}}|{{#var:i}}}}}}{{incvar|i}}{{#var:currentValue}}
{{#if:{{#var:currentValue}}|{{#vardefine:result|{{#replace:{{{1|}}}|[value]|{{#var:currentValue}}}}}}{{#var:result}}|}}
+
}}<noinclude>[[Category:Experimental templates]]
}}
 
<noinclude>[[Category:Experimental templates]]
 
 
A template for iterating through a list and doing something to each item.
 
A template for iterating through a list and doing something to each item.
  
Line 16: Line 12:
 
|list=Whoop, gloop, bloop, doop, shoop
 
|list=Whoop, gloop, bloop, doop, shoop
 
|delimiter=,
 
|delimiter=,
|<br/>
 
*[value]
 
 
}}
 
}}
 
</pre>
 
</pre>
Line 25: Line 19:
 
|list=Whoop, gloop, bloop, doop, shoop
 
|list=Whoop, gloop, bloop, doop, shoop
 
|delimiter=,
 
|delimiter=,
|<br/>
 
*[value]
 
 
}}
 
}}
  
Line 33: Line 25:
 
|list=Mega Drive-Master System-Saturn-Dreamcast
 
|list=Mega Drive-Master System-Saturn-Dreamcast
 
|delimiter=-
 
|delimiter=-
|[[[value]]]&nbsp;
 
 
}}
 
}}
 
</pre>
 
</pre>
Line 41: Line 32:
 
|list=Mega Drive-Master System-Saturn-Dreamcast
 
|list=Mega Drive-Master System-Saturn-Dreamcast
 
|delimiter=-
 
|delimiter=-
|[[[value]]]&nbsp;
 
 
}}
 
}}
 
</noinclude>
 
</noinclude>

Revision as of 14:39, 20 July 2018

A template for iterating through a list and doing something to each item.

Examples

{{foreach
|list=Whoop, gloop, bloop, doop, shoop
|delimiter=,
}}

Produces:

Whoopgloopbloopdoopshoop

{{foreach
|list=Mega Drive-Master System-Saturn-Dreamcast
|delimiter=-
}}

Produces:

Mega DriveMaster SystemSaturnDreamcast