Extension:Variables

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

Other languages:
Deutsch • ‎English • ‎Tiếng Việt • ‎español • ‎français • ‎polski • ‎português do Brasil • ‎русский • ‎中文 • ‎日本語 • ‎한국어
Manuel des extensions MediaWiki
OOjs UI icon advanced.svg
Variables
État de la version : stable
Implémentation Parser function
Description Introduces parser functions for dealing with page-scoped variables.
Auteur(s)
Maintainer(s) MGChecker
Dernière version 2.5.1 (2019-03-10)
MediaWiki 1.29+
PHP 5.5+
Changements
base de données
Non
Licence ISC License
Téléchargement
README
RELEASE-NOTES
Traduisez l’extension Variables sur translatewiki.net
Vérifiez la matrice des utilisations et des versions.
Rôle Vagrant variables
Problèmes Tâches ouvertes · Signalez un bug

L'extension Variables vous permet de définir une variable sur une page, de l'utiliser ensuite dans cette même page ou dans des modèles inclus dans la page, de changer sa valeur, notamment en utilisant son ancienne valeur, 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.

This extension will not be enabled for wikis run by the Wikimedia Foundation (WMF).[1][2][3][4] See alternatives.

Assigning a value to a variable

#vardefine

  • {{#vardefine:variablename|specifiedvalue}}

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

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

#vardefineecho

  • {{#vardefineecho:variablename|specifiedvalue}}

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

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

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

The value can be used in parser functions, etc.

#varexists

  • {{#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.

#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 | 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.

Exemples

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

Installation

  • Téléchargez et placez le(s) fichiers (s) dans un répertoire appelé Variables dans votre dossier extensions/.
  • Ajoutez le code suivant à la fin de votre fichier LocalSettings.php :
    wfLoadExtension( 'Variables' );
    
  • Yes Fait – Accédez à Special:Version sur votre wiki pour vérifier que l'extension a bien été installée.

Pour les utilisateurs de MediaWiki 1.30 ou précédents :

Les instructions ci-dessus décrivent la nouvelle procédure pour installer cette extension en utilisant wfLoadExtension(). Si vous avez besoin d'installer cette extension sur les précédentes versions de MediaWiki (1.30 ou antérieur), à la place de wfLoadExtension( 'Variables' );, vous devez utiliser :

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

Compatibility

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.

Alternatives

As this extension will not be enabled for wikis run by the Wikimedia Foundation (WMF)[1][2][3][4], here are some alternatives:

  • If you use Variables as cache for expensive operations, you can transform the section where you need them into a template and pass the required information as template parameters instead. This will work until you reach the expansion depth limit.
  • If you use Variables to do more complex template operations than possible with simple ParserFunctions, you can use Scribunto's Lua functionality instead. Note that this does not add support for global Variables. However, Extension:VariablesLua adds an Scribunto interface for the Variables store.
  • Don't use variables, instead duplicate the information you need as a variable. If that information is aquired by an expensive template call, performance may suffer. This will work until you reach the node count limit.

Voir aussi

References