Extension:Variables
| Language: | English • Deutsch • Tiếng Việt |
|---|
|
Variables Release status: stable |
|
|---|---|
| Implementation | Parser function |
| Description | Introduces parser functions for dealing with page-scoped variables. |
| Author(s) | Rob Adams, Tom Hempel, Xiloynaha, Daniel Werner |
| Last version | 2.0 (2011-11-16) |
| MediaWiki | 1.12+ (For older MW see Pre MW 1.12 subpage) |
| License | Public domain |
| Download | Download snapshot Subversion [Help] |
|
Check usage (experimental) |
|
Variables is a MediaWiki extension that 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 slowing down the wiki with huge numbers of templates. Combine this extension with the ParserFunctions extension for best results.
Contents |
[edit] Assigning a value to a variable
[edit] #vardefine
{{#vardefine:variablename|specifiedvalue}}
Assigns the value specifiedvalue to the (already existing or hereby introduced) variable variablename.
- Example: {{#vardefine:iconwidth|25}} making iconwidth = 25
[edit] #vardefineecho
{{#vardefineecho:variablename|specifiedvalue}}
Works exactly as #vardefine, but the affected value is printed.
[edit] 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. Before version 2.0 though, the default value always was expanded. From version 2.0 on the default only gets expanded in case it really is being needed.
The value can be used in parser functions, etc.
[edit] #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.
[edit] #var_final
Experimental new function introduced in Variables 2.0. 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, expecting the right value is being used. 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.
[edit] Examples
The ParserFunctions extension must also be installed to use #expr
Compute x = 2*a + b:
{{#vardefine:x|{{#expr:2*{{#var:a}}+{{#var:b}}}} }}
Add one to n:
{{#vardefine:n|{{#expr:{{#var:n}}+1}}}}
[edit] Compatibility
There are working versions for Variables extension down to MediaWiki 1.8 or even earlier. However, the most recent versions (1.4, 2.x) are only working with MW 1.12+. If you want to use Variables extension with older MW wikis, please see the Pre MW 1.12 subpage for information and download.
[edit] Installation
Once you have downloaded the code, place the Variables directory within your MediaWiki extensions directory. Then add the following code near the bottom of your LocalSettings.php file:
# Variables require_once( "$IP/extensions/Variables/Variables.php" );
[edit] Change log
Variables was written by Rob Adams who has released it into the public domain. It was ported to MediaWiki version 1.8 by Hempel. It was ported to MediaWiki 1.10 by Rob Adams. #vardefineecho was added by Xiloynaha, #varexists, #var_final and #var with an optional default value as well as further bug-fixes by Danwe who also has put it into mediawikis svn repository.
For detailed and most recent history see the RELEASE-NOTES
[edit] See also
- Extension:DynamicFunctions##arg:
- bugzilla:7865 - Enable VariablesExtension on Wikimedia wikis (status: wontfix)
- Extension:MyVariables - creates new variables in the MediaWiki sense.
- Extension:Control Structure Functions - ParserFunctions extension rehashed with loop functionality added.
- Extension:Loops
- Extension:ArrayExtension - create an array and provide array functions (such as search, split, and sort) and set operations (such as intersect, union and diff).
- Extension:HashTables - new parser functions for handling hash tables in MediaWiki.