Release status: stable | |
|---|---|
| Implementation | Parser function |
| Description | Introduces parser functions for dealing with page-scoped variables. |
| Author(s) |
|
| Maintainer(s) | MGChecker |
| Latest version | 2.6.0-beta (2023-12-05) |
| MediaWiki | 1.29+ |
| PHP | 5.5+ |
| Database changes | No |
| License | ISC License |
| Download | README RELEASE-NOTES |
| |
| Translate the Variables extension if it is available at translatewiki.net | |
| Vagrant role | variables |
| Issues | Open tasks ·Report a bug |
| This extension is incompatible with plans to parallelize parsing, as is intended by the use ofParsoid. Therefore, the future of this extension is uncertain, and it is expected to become incompatible with the standard MediaWiki parser within a few years. For further information, seeT250963 andNo support for sequential, in-order processing of extension tags. |
InternalParseBeforeSanitize hook forMediaWiki 1.35 -MediaWiki 1.38, or missing the#var_final parser function inMediaWiki 1.39+, please update to the latest version (master branch for now). SeeT276627 andT250963 for further information.TheVariables 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 slowing down the wiki with huge numbers of templates. Combine this extension with theParserFunctions extension for best results.
#vardefine{{#vardefine:variablename |specifiedvalue }}Assigns the valuespecifiedvalue to the (already existing or hereby introduced) variablevariablename.
{{#vardefine:iconwidth|25}} makingiconwidth = 25#vardefineecho{{#vardefineecho:variablename |specifiedvalue }}Works exactly as#vardefine, but the affected value is printed.
iconwidth = {{#vardefineecho:iconwidth|25}}#varThe value of the variablevariablename is produced by
{{#var:variablename}}If undefined, this produces an empty string; it does not give an error message.
It is possible to get a default 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 is actually used.
The value can be used in parser functions, etc.
#varexists{{#varexists:variablename }} returns1 if the variable is already defined. If the variable is not defined the return value is void.It supports a second and a third parameter to replace these values.
{{#varexists:variablename |if-value |else-value }}This is equivalent to:
{{#if: {{#varexists:variablename }} |if-value |else-value }}but it is much shorter and better arranged. Before version 2.5 though, both were always expanded.From version 2.5 on the if and else values only get expanded when their cases are actually entered.
#var_finalExperimental 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 |defaultvalue }}Thedefault 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.
TheParserFunctions extension must also be installed to use#expr.
Computex = 2*a + b:
{{#vardefine:x|{{#expr:2*{{#var:a}}+{{#var:b}}}}}}Add one ton:
{{#vardefine:n|{{#expr:{{#var:n}}+1}}}}Variables folder to yourextensions/ directory.cdextensions/gitclonehttps://gerrit.wikimedia.org/r/mediawiki/extensions/VariableswfLoadExtension('Variables');
This extension provides two configuration parameters:
$egVariablesAreVolatiletrue;$egVariablesDisabledFunctions[];[ 'var_final', 'vardefineecho' ];The recommended version of the Variables extension for recent MediaWiki releases is listed below. Older versions of the extensions might work as well, but aren't tested for new MediaWiki releases.
| MediaWiki version | Variables version |
|---|---|
| 1.19–1.22 | 2.1.x |
| 1.23–1.28 | 2.2.x |
| 1.29–1.34 | 2.5.x |
| 1.35–1.43 | master branch (2.6.0-beta) |
| 1.44+ | master branch,untested |
As this extension will not be enabled for wikis run by the Wikimedia Foundation (WMF), here are some alternatives:[1][2][3][4]
| This extension is included in the following wiki farms/hosts and/or packages:This is not an authoritative list. Some wiki farms/hosts and/or packages may contain this extension even if they are not listed here. Always check with your wiki farms/hosts or bundle to confirm. |