Extension:Loops

This page is a translated version of the page Extension:Loops and the translation is 32% complete.
Outdated translations are marked like this.
Manuel des extensions MediaWiki
Loops
État de la version : stable
Implémentation Fonction d'analyseur
Description Fournit des fonctions d'analyseur pour l'exécution de boucles
Auteur(s)
Dernière version 0.5.2 (2019-08-05)
MediaWiki 1.34+
PHP 5.6+
Modifie la base
de données
Non
Licence Licence publique générale GNU v2.0 ou supérieur
Téléchargement
README
RELEASE-NOTES
Exemple sandbox.semantic-mediawiki.org
  • $wgLoopsEnabledFunctions
  • $wgLoopsCountLimit
Téléchargements trimestriels 75 (Ranked 106th)
Traduire l’extension Loops sur translatewiki.net si elle y est disponible
Problèmes Tâches ouvertes · Signaler un bogue

Fournit des fonctions d'analyseur pour l'exécution de boucles

Currently, this extension is maintained on a basic level by MGChecker.

Utilisation

Les exemples suivants utilisent l'extension Variables et, dans certains cas, l'extension ParserFunctions .

#while

{{#while}} effectue une boucle (c'est-à-dire qu'il analyse plusieurs fois une instruction de bloc de balisage wiki donnée) tant que le balisage de condition s'évalue en non-espace.

{{#while:
 | <condition text>
 | <block statement>
}}
Exemples

Balisage Wiki :

{{#vardefine: i | 0 }}{{#while:
 | {{#ifexpr: {{#var: i }} < 5 | true }}
 | <nowiki />
* {{#var: i }}{{#vardefine: i | {{#expr: {{#var: i }} + 1 }} }}
}}

Faites ce qui suit :

  • 0
  • 1
  • 2
  • 3
  • 4

{{#while}} peut également être utilisé dans un modèle pour simuler un tableau numéroté. Si la page "Template:Loops Test" contient

{{#vardefine: i | 0 }}{{#while:
 | {{{arg{{#var: i }} |}}}
 | <nowiki />
* {{{arg{{#var: i }} }}}{{#vardefine: i
   | {{#expr: {{#var: i }} + 1 }}
 }}
}}

puis le wiki-markup

{{Loops Test
 |arg0=zero
 |arg1=one
 |arg2=two
 |arg3=three
 |arg4=four
}}

produit

  • zero
  • one
  • two
  • three
  • four

Il est important de noter que les espaces blancs, y compris les nouvelles lignes, les tabulations et les espaces, sont supprimés du début et de la fin de tous les arguments de ces fonctions d'analyse. Si cela n'est pas souhaitable, l'ajout de caractères autres que des espaces blancs (y compris le codage HTML pour un caractère d'espace blanc &#32;) empêchera la suppression (d'où les balises ‎<nowiki> dans les exemples ci-dessus).

#dowhile

{{#dowhile}} fonctionne exactement comme {{#while}}, à l'exception du fait que l'instruction block est garantie d'être analysée et affichée (si elle aboutit à un texte affichable) au moins une fois. Ceci est fait avant que le texte de condition soit évalué.

#loop

{{#loop: <variable name>
 | <starting value>
 | <number of loops to be performed>
 | <wiki markup>
}}

{{#loop}} analyse et affiche à plusieurs reprises <wiki markup> un nombre de fois égal à la valeur absolue de <number of loops to be performed>. <Starting value> est placé dans une variable (accessible par la fonction d'analyseur {{#var:}} de l'extension Variables ) en utilisant le nom <variable name>. Après chaque boucle, la variable est incrémentée d'un si <number of loops to be performed> est positif, ou diminuée d'un si <number of loops to be performed> est négatif.

From all loop functions, #loop should have the best performance since there is no condition which has to be expanded and validated for each cycle.
Exemples

Le code suivant :

{{#loop: varname
 | 4
 | 4
 | <nowiki />
* This is round {{#var: varname }} and we have {{#expr: 7 - {{#var: varname }} }} more to go
}}

produces

  • This is round 4 and we have 3 more to go
  • This is round 5 and we have 2 more to go
  • This is round 6 and we have 1 more to go
  • This is round 7 and we have 0 more to go

#forargs (experimental)

{{#forargs}} is to be used in templates.

It takes arguments that are passed to the template and puts them in variables accessible by Variables extension's {{#var:}} parser function.

{{#forargs: <prefix>
 | <key>
 | <value>
 | <block statement>
}}

This function iterates through each argument whose name begins with <prefix>.

With each iteration it puts the argument name minus <prefix> into <key> as if calling {{#vardefine: <key> }}.

It then takes the value of the argument and puts it into <value> in a similar method.

The block statement is then expanded.

The block statement may contain {{#var: <key> }} and {{#var: <value> }} to access the stored arguments.

Example

If the page "Template:Loops Test" contains

{{#forargs: arg
 | key
 | value
 | <nowiki />
* {{#var: key }} = {{#var: value }}
}}

then the wiki markup

{{Loops Test
 | arg1=val1
 | spam=spammity
 | arg5=val5
 | argument=value
}}

produces

  • 1 = val1
  • 5 = val5
  • ument = value

#fornumargs (experimental)

{{#fornumargs: <key>
 | <value>
 | <block statement>
}}

{{#fornumargs}} performs similarly to {{#forargs}} with two major differences: It doesn't take a prefix argument, and it only works on numbered arguments whether they're explicitly numbered,

{{Template
 | 1=one
 | 2=two
}}

or implicitly numbered.

{{Template
 | one
 | two
}}

Mixing these methods in a single template call may cause values to get overwritten, so be careful.

Examples

If "Template:Loops Test" is edited to contain:

{{#fornumargs: number
 | value
 | <nowiki />
* {{#var: number }} = {{#var: value }}
}}

then

{{Loops Test
  | Apricot
  | B = Bolognese
  | Caramel slice
  | 5 = Eclair
}}

will result in

  • 1 = Apricot
  • 2 = Caramel slice
  • 5 = Eclair

Installation

Some of the parser functions provided by this extension require the ParserFunctions as well as the Variables extension to be installed first.

Configuration

These configuration variables have to be set in the LocalSettings.php file after the inclusion of this extension.

ExtLoops::$maxLoops ($egLoopsCountLimit)
This parameter sets the maximum number of loops a page is allowed to perform (default 100). Setting it to -1 lets the loops run within the limits of phps environment. This parameter affects neither the {{#forargs:}} nor {{#fornumargs:}} parser functions.
$egLoopsEnabledFunctions
Configuration variable (array) to define which Loops functions should be enabled. By default, all functions are enabled if the Variables extension is installed as well. If the Variables extension is not installed, #loop, #forargs and #fornumargs will be disabled since they do not work without it being installed. To enable the #fornumargs and #forargs functions only, one can use:
$egLoopsEnabledFunctions = array_diff(
    $egLoopsEnabledFunctions, [
        'forargs', 'fornumargs'
        ]
    );


Voir aussi