Template:Infobox Var/doc: Difference between revisions
(Created page with "{{Documentation}} {{invokes|Infobox Item}} {{ToC|left}} {{clear}} {{Standard infobox parameters}} ==Parameters== ===type=== The type of Var, either "Varbit" or "Varplayer". ===index=== The index of the Var, which should always be a number. ===name=== The name of the Varbit or Varplayer. There are no official names, but names should generally reflect the usage of the Var. This param is optional, and if it is not included, the name defaults to "Varbit X" or "Varplayer...") |
No edit summary |
||
Line 1: | Line 1: | ||
{{ |
{{documentation}} |
||
{{invokes|Infobox Item}} |
|||
{{ToC|left}} |
|||
{{clear}} |
|||
The '''Var''' template is used to store data in the cache for later use. |
|||
{{Standard infobox parameters}} |
|||
== |
==Usage== |
||
<pre>{{Var|name|data}}</pre> |
|||
=== |
===Name=== |
||
The |
The name of the variable. Case sensitive. |
||
=== |
===Data=== |
||
The data to be stored in the variable. The literal value is stored and is not evaluated (unless you also store the appropriate functions) - if you store 6*7, when you recall it you will get the result <code>6*7</code>, not 42; if you store <code><nowiki>{{#expr:6*7}}</nowiki></code> you ''will'' get the result <code>42</code>. |
|||
The index of the Var, which should always be a number. |
|||
== |
==Recalling data== |
||
Recalling data is done by use of the #var parser function: |
|||
The name of the Varbit or Varplayer. There are no official names, but names should generally reflect the usage of the Var. This param is optional, and if it is not included, the name defaults to "Varbit X" or "Varplayer X". If used on mw.pages other than <nowiki>RS:Varbit/X</nowiki> or <nowiki>RS:Varplayer/X</nowiki>, note that this param should be set in order to avoid strange default names being generated from the title of the page. |
|||
<pre>{{#var:name}}</pre> |
|||
where ''name'' is the same named used in the var template above. |
|||
==Expansion on usage== |
|||
===content=== |
|||
Variables can be redefined where needed - you can use the Var template many times. |
|||
The content that the Var is used for, as a link. If the Var is used in multiple pieces of content, this should be explained in prose with this param set to "Multiple". If the Var is used in a way that would not be considered content, this param should be set to "Other". |
|||
Variables can be called where needed - you can use the #var parser function many times. |
|||
===class=== |
|||
The class of the Var, which describes how the Var's value is interpreted. The following classes are supported with categories. |
|||
Variables can be defined recursively, that is defined as a function as themselves. For example: |
|||
* '''Enum''' - Denotes a Var where each value is interpreted as a different state. |
|||
<pre>{{Var|i|0}} |
|||
* '''Switch''' - Denotes a Var with two values, typically 0 and 1, where one state is interpreted as ON and the other OFF. |
|||
* '''Bitmap''' - Denotes a Var where the value of the Var is not interpreted as a single unit. Instead, each bit or groups of bits in the Var are interpreted separately. |
|||
{{Var|i|{{#expr:{{#var:i}}+1}}}}</pre> |
|||
* '''Counter''' - Denotes a Var where the value is interpreted as a counter, usually counting up or down incrementally. |
|||
The first line initialises the variable i to 0 (required to prevent expression errors), and the second will increment i by one each time it is used. If you place the second line into a template that is used multiple times on a page e.g. a table row template, you can recall i to find something that may be of importance in context e.g. the number of rows in the template. As an additional note, the increment doesn't have to be 1, it can be any number - it doesn't even have to be a fixed number, it can be a function of some other parameters in the template. |
|||
* '''Other''' - Denotes a Var which does not fall into any of the above categories. |
|||
==Rationale== |
|||
''"But I know how to use the #vardefine parser function. Isn't this the exact same thing?"'' |
|||
Yes, it is. The Var templates uses the #vardefine parser function to define the variables. So what's the point of this template? |
|||
A problem with the design of the variables extension is that vardefine is always executed even if it is on the false path of a conditional parser function: for example, if you created a construct such as |
|||
<pre>{{#ifexpr: ... |
|||
|{{#vardefine:a|b}} |
|||
|{{#vardefine:a|c}} |
|||
}}</pre> |
|||
Variable a will now always hold c, regardless of whether the expression is true or false. |
|||
There are two ways around this. The first is to move the #vardefine to the outside of the conditional parser: following the previous example, the required construct would be similar to |
|||
<pre>{{#vardefine:a|{{#ifexpr: ... |b|c}}}}</pre> |
|||
However, sometimes you can't move the #vardefine outside, it logically makes more sense or is easier to follow to leave the #vardefine on the inside. This is where the Var template comes in. As the template is not expanded if it is on the false path, the #vardefine is not present and cannot define variables where it shouldn't. Thus a construct like this will also work for the above example: |
|||
<pre>{{#ifexpr: ... |
|||
|{{Var|a|b}} |
|||
|{{Var|a|c}} |
|||
}}</pre> |
|||
==See also== |
|||
[[mw:Extension:VariablesExtension|The Variables Extension page on MediaWiki]] |
|||
<includeonly>[[Category:Transclusion templates|{{PAGENAME}}]]</includeonly> |
Revision as of 00:55, 17 October 2024
The Var template is used to store data in the cache for later use.
Usage
{{Var|name|data}}
Name
The name of the variable. Case sensitive.
Data
The data to be stored in the variable. The literal value is stored and is not evaluated (unless you also store the appropriate functions) - if you store 6*7, when you recall it you will get the result 6*7
, not 42; if you store {{#expr:6*7}}
you will get the result 42
.
Recalling data
Recalling data is done by use of the #var parser function:
{{#var:name}}
where name is the same named used in the var template above.
Expansion on usage
Variables can be redefined where needed - you can use the Var template many times.
Variables can be called where needed - you can use the #var parser function many times.
Variables can be defined recursively, that is defined as a function as themselves. For example:
{{Var|i|0}} {{Var|i|{{#expr:{{#var:i}}+1}}}}
The first line initialises the variable i to 0 (required to prevent expression errors), and the second will increment i by one each time it is used. If you place the second line into a template that is used multiple times on a page e.g. a table row template, you can recall i to find something that may be of importance in context e.g. the number of rows in the template. As an additional note, the increment doesn't have to be 1, it can be any number - it doesn't even have to be a fixed number, it can be a function of some other parameters in the template.
Rationale
"But I know how to use the #vardefine parser function. Isn't this the exact same thing?"
Yes, it is. The Var templates uses the #vardefine parser function to define the variables. So what's the point of this template?
A problem with the design of the variables extension is that vardefine is always executed even if it is on the false path of a conditional parser function: for example, if you created a construct such as
{{#ifexpr: ... |{{#vardefine:a|b}} |{{#vardefine:a|c}} }}
Variable a will now always hold c, regardless of whether the expression is true or false.
There are two ways around this. The first is to move the #vardefine to the outside of the conditional parser: following the previous example, the required construct would be similar to
{{#vardefine:a|{{#ifexpr: ... |b|c}}}}
However, sometimes you can't move the #vardefine outside, it logically makes more sense or is easier to follow to leave the #vardefine on the inside. This is where the Var template comes in. As the template is not expanded if it is on the false path, the #vardefine is not present and cannot define variables where it shouldn't. Thus a construct like this will also work for the above example:
{{#ifexpr: ... |{{Var|a|b}} |{{Var|a|c}} }}
See also
The Variables Extension page on MediaWiki