Extension:Loops

From Dolphin Emulator Wiki
Jump to navigation Jump to search

The Loops extension provides parser functions for performing loops.

Usage

#while

{{#while}} performs a loop (i.e. it repeatedly parses a given wiki markup block statement) so long as the condition mark-up evaluates to non-whitespace.

{{#while:|<condition text>|
  <block statement>
}}
Examples

The wiki markup:

{{#vardefine:i|0}}
<ul>
{{#while:|{{#ifexpr:{{#var:i}}<5|true}}|
  <li>{{#var:i}}</li>
  {{#vardefine:i|{{#expr:{{#var:i}}+1}}}}
}}
</ul>

produces the following:

  • 0
  • 1
  • 2
  • 3
  • 4

#dowhile

{{#dowhile}} performs exactly like {{#while}}, with the exception that the block statement is guaranteed to be parsed and displayed (if it results in displayable text) at least once. This is done before the condition text is evaluated.

#loop

{{#loop:<variable name>
  |<starting value>
  |<number of loops to be performed>
  |<block statement>
}}

{{#loop}} repeatedly parses and displays the block statement a number of times equal to the absolute value of <number of loops to be performed>. <Starting value> is placed in a variable (accessible by {{#var:<variable name>}} parser function) using the name <variable name>. After each loop, the variable is incremented by one if <number of loops to be performed> is positive, or decremented by one if <number of loops to be performed> is negative.

Note: of all the loop functions, #loop should have the best performance, since there is no condition which has to be expanded and validated for each cycle.

Examples

The following code:

<ul>
{{#loop:varname
  |4
  |4
  |<li>This is round {{#var:varname}} and we have {{#expr:7-{{#var:varname}}}} more to go</li>
}}
</ul>

produces

  • This is round 4 and we have 3 more to go
  • This is round 5 and we have 2 more to go
  • This is round 6 and we have 1 more to go
  • This is round 7 and we have 0 more to go

#forargs

{{#forargs}} is to be used in templates. It takes arguments that are passed to the template and puts them in variables accessible by {{#var:<variable name>}} parser function.

{{#forargs:<prefix>
  |<key>
  |<value>
  |<block statement>
}}

This function iterates through each argument whose name begins with <prefix>. With each iteration it puts the argument name minus <prefix> into <key> as if calling {{#vardefine:<key>}}. It then takes the value of the argument and puts it into <value> in a similar method. The block statement is then expanded. The block statement may contain {{#var:<key>}} and {{#var:<value>}} to access the stored arguments.

Example

If the page "Template:Loops Test" contains

<ul>
{{#forargs:arg
  |key
  |value
  |<li>{{#var:key}} = {{#var:value}}</li>
}}
</ul>

then the wiki markup

{{Loops Test
|arg1     = val1
|spam     = spammity
|arg5     = val5
|argument = value
}}

produces

  • 1 = val1
  • 5 = val5
  • ument = value

#fornumargs

{{#fornumargs:<key>
  |<value>
  |<block statement>
}}

{{#fornumargs}} performs similarly to {{#forargs}} with two major differences: it doesn't take a prefix argument, and it only works on numbered arguments whether they're explicitly numbered:

{{Template|1=one|2=two}}

or implicitly numbered:

{{Template|one|two}}

Note: mixing these methods in a single template call may cause values to get overwritten.

Examples

If "Template:Loops Test" is edited to contain:

<ul>
{{#fornumargs:number
  |value
  |<li>{{#var:number}} = {{#var:value}}</li>
}}
</ul>

then

{{Loops Test
|Apricot
|B = Bolognese
|Caramel slice
|5 = Eclair
}}

will result in

  • 1 = Apricot
  • 2 = Caramel slice
  • 5 = Eclair

Notes

Arrays

Loops can also be used in a template to simulate a numbered array, in this case using {{#while}} loops. If the page "Template:Loops Test" contains

{{#vardefine:i|0}}
<ul>
{{#while:|{{{arg{{#var:i}}|}}}|
  <li>{{{arg{{#var:i}}}}}</li>
  {{#vardefine:i|{{#expr:{{#var:i}}+1}}}}
}}
</ul>

then the wiki-markup

{{Loops Test
|arg0 = zero
|arg1 = one
|arg2 = two
|arg3 = three
|arg4 = four
}}

produces

  • zero
  • one
  • two
  • three
  • four

Notice how a variable is being used in the name of an argument in {{{arg{{#var:i}}}}}. 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:

{{#vardefine:i|0}}
{{#while:|{{#ifexpr:{{#var:i}}<5|true}}|
  {{#vardefine:member{{#var:i}}|{{#var:i}}}}
  {{#vardefine:i|{{#expr:{{#var:i}}+1}}}}
}}

This would create 5 variables named member0, member1, member2, member3, and member4, 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:

{{#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>
}}

The above two examples together produces the following output:

  • 0
  • 1
  • 2
  • 3
  • 4

In this case, the same page output is achieved as the first example under {{#while}}, 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

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/> 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).