Extension:Variables

From Dolphin Emulator Wiki
Jump to: navigation, search

The Variables extension allows you to define a variable on a page, use it later in that same page or included templates, change its value, possibly to a value given by an expression in terms of the old value, etc.

It is much like a template, only very lightweight and scoped to only a single page, so you can use many variables on a page without complicating the wiki with huge numbers of templates.

Assigning a value to a variable

#vardefine

  • {{#vardefine:variablename|specifiedvalue}}

Assigns the value specifiedvalue to the (already existing or hereby introduced) variable variablename.

  • Example: {{#vardefine:iconwidth|25}} making iconwidth = 25

#vardefineecho

  • {{#vardefineecho:variablename|specifiedvalue}}

Works exactly as #vardefine, but the affected value is printed.

  • Example: making iconwidth = {{#vardefineecho:iconwidth|25}}

Retrieving the value of a variable

#var

The value of the variable variablename is produced by

  • {{#var:variablename}}

If undefined, this produces an empty string; it does not give an error message.

It is possible to define a value for the case that the variable is undefined or void:

  • {{#var:variablename|defaultvalue}}

This is equivalent to:

  • {{#if:{{#var:variablename}}|{{#var:variablename}}|defaultvalue}}

but it is much shorter and better arranged. The default value only gets expanded in case it is actually used. The value can be used in parser functions, etc.

#varexists

  • {{#varexists:variablename}} returns 1 if the variable is already defined (also when the value is a void string). If the variable is not defined the return value is void.

#var_final

This function will output the final, last value a variable has at the end of the page rendering. Naturally, the value will be inserted after the parser went over the entire wiki markup, so this function can't be used in other functions. Example:

  • {{#var_final:variablename|default}}

The default value will be used if the variable doesn't exist at the final page rendering stage or if its value is an empty string. The default will be expanded right where the function is used, so the parameter will be expanded, even if it won't be needed.

Examples

Compute x = 2*a + b:

  • {{#vardefine:x|{{#expr:2*{{#var:a}}+{{#var:b}}}}}}

Add one to n:

  • {{#vardefine:n|{{#expr:{{#var:n}}+1}}}}

Swap the value of two variables c and q:

  • {{#vardefine:c|{{#var:q}}{{#vardefine:q|{{#var:c}}}}}}