Manual:Variable


 * This page is about creating variables. For help using default variables, see Help:Variables.

Variables are bits of wikitext that look like templates but have no parameters and have been assigned hard-coded values. Standard wiki markup such as  or   are examples of variables. You can also extend wiki markup by defining your own custom variables.

The term is something of a misnomer because there is nothing variable about a variable. End users cannot change the value of a variable because it is predetermined by a bundle of php code that calculates its value. The term "variables" comes from the source of their value: a php variable or something that could be assigned to a variable, e.g. a string, a number, an expression, or a function return value.

Defining custom variables
Variables are a special case of magic words so our first step will be to define the variable as a magic word:


 * 1) Choose a magic word id for your variable.  This is only an internal identifier that is used to tie together the various parts of the variable definition: the names that appear in wiki text, and the php code that assigns a value to the variable. It is best to choose an id that will be uniquely associated with your extension and is unlikely to be confused with other magic word ids used by other extensions.  A common strategy is to use something like   where canonicalname is the name under which you will register your extension (see Registering custom variables below).
 * 2) Define the names that will appear in wiki text. To accomplish this, you will need to define and register a hook function with LanguageGetMagic.  Your hook function will be passed an array and the hook function will add an entry whose key is the internal magic word id and whose value is an array containing all the names you want to use for your variable.  Your names can be case sensitive and language dependent.  See LanguageGetMagic and magic words for more information.
 * 3) Provide php code to assign a value to the variable.  To accomplish this, you will need to define and register a hook function with Manual:Hooks/ParserGetVariableValueSwitch.

Note that the only difference between this process and the general process for defining magic words is the last step: defining a hook function for Manual:Hooks/ParserGetVariableValueSwitch. Parser functions have a different method for associating an id with php code. See Manual:Parser functions for more information.

Registering custom variables
This is a two step process:


 * 1) Define the variable so that it gets included in  . This requires adding a member to $wgExtensionCredits.  For more information, please see Registering features with MediaWiki.
 * 2) Declare the magic word id as a variable.  To accomplish this we write and assign a hook to , the subject of this article.  Note: this hook is only for use with variables. Do not use it to define parser functions.

Example
''Note: if you wish to use this example as a coding template, please replace  with something unique to your project so that there is less risk of name clashes with MediaWiki or any of its extensions. For example, if your extension was named  and you were reasonably sure that no-one else had an extension with constants, variables, functions, or classes beginning 'BEST_', 'wgBEST', 'wfBEST', or even 'BEST' you might want to replace 'My' with 'BEST'.''

Extensions with multiple variables
You may, of course, define more than one variable for an extension. Repeating the above code for each variable may be a bit tedious. Below is a method that lets you define a data structure with all the data about all the variables in one place. The code following uses that data to set up the registration.

Separating code and data in this fashion has several advantages:
 * everything is in one place if you need to change it
 * its easy to get a quick overview of all your extension's variables

The main disadvantage is that the code is a bit abstract and may be hard for a new php programmer to understand.

Note: the code below is presented for concept purposes only: it has not been tested for syntax errors - use as a template at your own risk.

The code above is just one approach to the problem of multiple variables. For an alternative approach to group definition and registration, see Extension:Variables.

For more information

 * Help:Variables - discusses default variables built into the core MediaWiki package
 * Manual:Magic words - reviews the different kinds of magic words and how MediaWiki tells apart variables, parser functions, and templates.