From MediaWiki.org
Jump to navigation Jump to search
This page is a translated version of the page Extension:Variables and the translation is 10% complete.

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎Tiếng Việt • ‎català • ‎dansk • ‎español • ‎français • ‎italiano • ‎polski • ‎português • ‎português do Brasil • ‎sicilianu • ‎suomi • ‎русский • ‎中文 • ‎日本語 • ‎한국어
Manual de extensões do MediaWiki
OOjs UI icon advanced.svg
Estado da versão: estável
Implementação Parser function
Descrição Introduces parser functions for dealing with page-scoped variables.
Responsável MGChecker
Última versão 2.5.1 (2019-03-10)
MediaWiki 1.29+
PHP 5.5+
Alterações à
base de dados
Licença ISC License
Exportação (Download)
Hooks usados
Traduza a extensão Variables se ela estiver disponível na wiki translatewiki.net
Verifique a matriz de utilização e versões
Função no Vagrant variables
Problemas Tarefas em aberto · Reportar um problema

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


  • {{#vardefine:variablename|specifiedvalue}}

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

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


  • {{#vardefineecho:variablename|specifiedvalue}}

Works exactly as #vardefine, but the affected value is printed.

  • Exemplo: making iconwidth = <code>{{#vardefineecho:iconwidth|25}}</code>

Retrieving the value of a variable


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:variablename }} returns 1 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.


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

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.


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


  • Exporte e coloque o ficheiro, ou ficheiros, num diretório chamado Variables, na sua pasta extensions/.
  • Acrescente o seguinte código ao fundo do ficheiro LocalSettings.php:
    wfLoadExtension( 'Variables' );
  • Yes Pronto – Na página especial Special:Version da sua wiki verifique se a extensão foi instalada.

Para utilizadores que tenham o MediaWiki versão 1.30 ou anteriores:

As instruções acima descrevem a nova forma de instalar esta extensão, usando a extensão wfLoadExtension() Se precisar de instalar esta extensão nestas versões anteriores (MediaWiki 1.30 e anteriores), em vez de wfLoadExtension( 'Variables' );, use:

require_once "$IP/extensions/Variables/Variables.php";


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.


This extension will not be enabled for wikis run by the Wikimedia Foundation (WMF). See phab:T9865, phab:T65324, phab:T113859, and phab:T151192.

Ver também