Extension:Variables

From MediaWiki.org
(Redirected from Extension:VariablesExtension)
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
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
Latest version 2.0.1 (2011-12-15)
MediaWiki 1.12+
Database changes No
License Public domain
Download
README
Hooks used
ParserFirstCallInit

ParserClearState
InternalParseBeforeLinks
InternalParseBeforeSanitize

Translate the Variables extension if possible

Check usage and version matrix; code metrics

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 slowing down the wiki with huge numbers of templates. Combine this extension with the ParserFunctions extension for best results.

Assigning a value to a variable[edit | edit source]

#vardefine[edit | edit source]

  • {{#vardefine:variablename|specifiedvalue}}

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

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

#vardefineecho[edit | edit source]

  • {{#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)[edit | edit source]

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 is actually used.

The value can be used in parser functions, etc.

#varexists[edit | edit source]

  • {{#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[edit | edit source]

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.

Examples[edit | edit source]

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}}}}

Download[edit | edit source]

You can download the extension directly from the MediaWiki source code repository (browse code). You can get:

One of the extensions tags

Not all extensions have tags. Some extensions have tags for each release, in which case those tags have the same stability as the release. To download a tag

  • Go to the tags list
  • Click the name of the tag you want to download
  • Click "snapshot"
The latest version of one of the extensions branches

Each extension has a master branch containing the latest code (might be unstable). Extensions can have further branches as well.

  • Go to the branches list
  • Click the branch name
  • Click "snapshot"
A snapshot made during the release of a MediaWiki version.

This might be unstable and is not guaranteed to work with the associated MediaWiki version.

After you've got the code, save it into the extensions/Variables directory of your wiki.

If you are familiar with git and have shell access to your server, you can obtain the extension, with all its tags and branches, as follows:

cd extensions
git clone https://gerrit.wikimedia.org/r/p/mediawiki/extensions/Variables.git

Installation[edit | edit source]

  • Download and extract the files in a directory called Variables in your extensions/ folder. If you're a developer and this extension is in a Git repository, then instead you should clone the repository.
  • Add the following code at the bottom of your LocalSettings.php:
require_once( "$IP/extensions/Variables/Variables.php" );
  • Done! Navigate to "Special:Version" on your wiki to verify that the extension is successfully installed.

Change log[edit | edit source]

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 optional default value as well as the 2.0 rewrite were contributed by Daniel Werner who is currently maintaining the extension.

For detailed and most recent history see the RELEASE-NOTES.

See also[edit | edit source]

Language: English  • Deutsch • 日本語 • Tiếng Việt