Extension:Loops: Difference between revisions

move arrays stuff to its own section for clarity
(extensive reformatting and some grammatical corrections. all the expanded code examples were changed to a more C-esque style without the weird spacing. more examples were added for complex array scenarios.)
(move arrays stuff to its own section for clarity)
 
Line 28: Line 28:


{{#vardefine:i|0}}<ul>{{#while:|{{#ifexpr:{{#var:i}}<5|true}}|<li>{{#var:i}}</li>{{#vardefine:i|{{#expr:{{#var:i}}+1}}}}}}</ul>
{{#vardefine:i|0}}<ul>{{#while:|{{#ifexpr:{{#var:i}}<5|true}}|<li>{{#var:i}}</li>{{#vardefine:i|{{#expr:{{#var:i}}+1}}}}}}</ul>
'''<nowiki>{{#while}}</nowiki>''' can also be used in a template to simulate a numbered array.  If the page "Template:Loops Test" contains
<pre>
{{#vardefine:i|0}}
<ul>
{{#while:|{{{arg{{#var:i}}|}}}|
  <li>{{{arg{{#var:i}}}}}</li>
  {{#vardefine:i|{{#expr:{{#var:i}}+1}}}}
}}
</ul>
</pre>
then the wiki-markup
<pre>
{{Loops Test
|arg0 = zero
|arg1 = one
|arg2 = two
|arg3 = three
|arg4 = four
}}
</pre>
produces
<ul><li>zero</li><li>one</li><li>two</li><li>three</li><li>four</li></ul>
Notice how a variable is being used in the name of an argument in <code><nowiki>{{{arg{{#var:i}}}}}</nowiki></code>. This is completely valid in MediaWiki syntax, and is what makes array-style storage possible. In fact, variables with names dependent on other variables can be created dynamically. Take the following as an example:
<pre>
{{#vardefine:i|0}}
{{#while:|{{#ifexpr:{{#var:i}}<5|true}}|
  {{#vardefine:member{{#var:i}}|{{#var:i}}}}
  {{#vardefine:i|{{#expr:{{#var:i}}+1}}}}
}}
</pre>
This would create 5 variables named <code>member0</code>, <code>member1</code>, <code>member2</code>, <code>member3</code>, and <code>member4</code>, and assign them each the values 0, 1, 2, 3, and 4 respectively. These members can then be accessed in a similar way to before:
<pre>
{{#if:{{#var:member0}}|
  {{#vardefine:i|0}}
  <ul>
  {{#while:|{{#var:member{{#var:i}}}}|
    <li>{{#var:member{{#var:i}}}}</li>
    {{#vardefine:i|{{#expr:{{#var:i}}+1}}}}
  }}
  </ul>
}}
</pre>
The above two examples together produces the following output:
{{#vardefine:i|0}}{{#while:|{{#ifexpr:{{#var:i}}<5|true}}|{{#vardefine:member{{#var:i}}|{{#var:i}}}}{{#vardefine:i|{{#expr:{{#var:i}}+1}}}}}}{{#if:{{#var:member0}}|{{#vardefine:i|0}}<ul>{{#while:|{{#var:member{{#var:i}}}}|<li>{{#var:member{{#var:i}}}}</li>{{#vardefine:i|{{#expr:{{#var:i}}+1}}}}}}</ul>}}
In this case, the same page output is achieved as the first example, however twice the loops and 6x as many variable definitions are used in the process. Since there's a maximum 200 loop limit per page, it's important to try and think first about exactly what is being accomplish to avoid unnecessary loop usage. The main use for this kind of dynamic variable naming would be for cases where swapping of the contents of each variable is necessary (such as sorting algorithms).


=== #dowhile ===
=== #dowhile ===
Line 215: Line 157:


== Notes ==
== Notes ==
=== Arrays ===
Loops can also be used in a template to simulate a numbered array, in this case using <nowiki>{{#while}}</nowiki> loops. If the page "Template:Loops Test" contains
<pre>
{{#vardefine:i|0}}
<ul>
{{#while:|{{{arg{{#var:i}}|}}}|
  <li>{{{arg{{#var:i}}}}}</li>
  {{#vardefine:i|{{#expr:{{#var:i}}+1}}}}
}}
</ul>
</pre>
then the wiki-markup
<pre>
{{Loops Test
|arg0 = zero
|arg1 = one
|arg2 = two
|arg3 = three
|arg4 = four
}}
</pre>
produces
<ul><li>zero</li><li>one</li><li>two</li><li>three</li><li>four</li></ul>
Notice how a variable is being used in the name of an argument in <code><nowiki>{{{arg{{#var:i}}}}}</nowiki></code>. This is completely valid in MediaWiki syntax, and is what makes array-style storage possible. In fact, variables with names dependent on other variables can be created dynamically. Take the following as an example:
<pre>
{{#vardefine:i|0}}
{{#while:|{{#ifexpr:{{#var:i}}<5|true}}|
  {{#vardefine:member{{#var:i}}|{{#var:i}}}}
  {{#vardefine:i|{{#expr:{{#var:i}}+1}}}}
}}
</pre>
This would create 5 variables named <code>member0</code>, <code>member1</code>, <code>member2</code>, <code>member3</code>, and <code>member4</code>, and assign them each the values 0, 1, 2, 3, and 4 respectively. These members can then be accessed in a similar way to before:
<pre>
{{#if:{{#var:member0}}|
  {{#vardefine:i|0}}
  <ul>
  {{#while:|{{#var:member{{#var:i}}}}|
    <li>{{#var:member{{#var:i}}}}</li>
    {{#vardefine:i|{{#expr:{{#var:i}}+1}}}}
  }}
  </ul>
}}
</pre>
The above two examples together produces the following output:
{{#vardefine:i|0}}{{#while:|{{#ifexpr:{{#var:i}}<5|true}}|{{#vardefine:member{{#var:i}}|{{#var:i}}}}{{#vardefine:i|{{#expr:{{#var:i}}+1}}}}}}{{#if:{{#var:member0}}|{{#vardefine:i|0}}<ul>{{#while:|{{#var:member{{#var:i}}}}|<li>{{#var:member{{#var:i}}}}</li>{{#vardefine:i|{{#expr:{{#var:i}}+1}}}}}}</ul>}}
In this case, the same page output is achieved as the first example under <nowiki>{{#while}}</nowiki>, however twice the loops and 6x as many variable definitions are used in the process. Since there's a maximum 200 loop limit per page, it's important to try and think first about exactly what is being accomplish to avoid unnecessary loop usage. The main use for this kind of dynamic variable naming would be for cases where swapping of the contents of each variable is necessary (such as sorting algorithms).
=== Whitespace and Page Output ===
=== Whitespace and Page Output ===
It's important to note that any trailing whitespace touching either a | pipe character or the inside of a template/extension call's brackets is stripped during parsing. This includes newlines, tabs, and spaces. If this is not desirable, adding any non-whitespace characters will prevent further stripping. An easy way to achieve this is with a <nowiki><nowiki/></nowiki> tag, since it will otherwise have no affect on page output. However, other than this automatic culling, no other culling is done for extra spaces outside of brackets or between characters, meaning that in practice most templates will need to be scrunched up to avoid display problems (including the examples here).
It's important to note that any trailing whitespace touching either a | pipe character or the inside of a template/extension call's brackets is stripped during parsing. This includes newlines, tabs, and spaces. If this is not desirable, adding any non-whitespace characters will prevent further stripping. An easy way to achieve this is with a <nowiki><nowiki/></nowiki> tag, since it will otherwise have no affect on page output. However, other than this automatic culling, no other culling is done for extra spaces outside of brackets or between characters, meaning that in practice most templates will need to be scrunched up to avoid display problems (including the examples here).


[[Category:Extension Docs]]
[[Category:Extension Docs]]
6,906

edits