How To Use Variables: Difference between revisions
imported>Tlosk (Created page with '{{RightToC}} {{Related|Help:Contents|Wiki Templates}} This function allows you to define a variable on a page, use it later in that same page, change its value, possibly to a va...') |
imported>Tlosk (Created page with '{{RightToC}} {{Related|Help:Contents|Wiki Templates}} This function allows you to define a variable on a page, use it later in that same page, change its value, possibly to a va...') |
(No difference)
|
Latest revision as of 09:33, 20 May 2009
Related topics: Help:Contents, Wiki Templates
This function allows you to define a variable on a page, use it later in that same page, change its value, possibly to a value given by an expression in terms of the old value, etc.
It's much like a template, only very lightweight and scoped to only a single page, so you can use many variables on a page without polluting the wiki with huge numbers of templates.
- This function requires the Variables Extension be installed.
Assigning a value to a variable
#vardefine
- {{#vardefine:variablename|specifiedvalue}}
assigns the value specifiedvalue to the (already existing or hereby introduced) variable variablename.
#vardefineecho
- {{#vardefineecho:variablename|specifiedvalue}}
works exactly as #vardefine, but the affected value is printed.
Retrieving the value of a variable (#var)
The value of the variable variablename is produced by
- {{#var:variablename}}
If undefined, this produces the empty string; it does not give an error message.
It's 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's much shorter and better arranged.
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.
Examples
- Note that the ParserFunctions extension must also be installed to use #expr
Compute 2*a + b:
- {{#expr:2*{{#var:a}}+{{#var:b}}}}
Add one to n:
- {{#vardefine:n|{{#expr:{{#var:n}}+1}}}}