Help:Array (older methods)

This page is an extension of Help:Array and explains various older methods of creating a kind of associative array using templates.

Note that producing an array element for a given index value corresponds to a case statement or switch.

Array technique using parameter defaultsEdit

Template:Short DOW par def, based on parameter defaults, contains (without the newlines):


{{short DOW par def|ifu=|3=}} gives Wed

{{short DOW par def|ifu=|6=|3=|5=|3=}} gives WedFriSat

The results are in the index order according to the template content, not in the order of the parameters in the template call. Multiple occurrences are removed.

See also Template:M to ft par def.

Array technique using CSSEdit

Unlike other methods, this method works on the client site. The result cannot be used in expressions for template names, parameter names, parameter values, page names in links, etc.


Template:Short DOW css d contains (without the newlines):

<span style="display:non{{{1|e}}}">Mon</span>
<span style="display:non{{{2|e}}}">Tue</span>
<span style="display:non{{{3|e}}}">Wed</span>
<span style="display:non{{{4|e}}}">Thu</span>
<span style="display:non{{{5|e}}}">Fri</span>
<span style="display:non{{{6|e}}}">Sat</span>
<span style="display:non{{{0|e}}}">Sun</span>
<code>{{short DOW css d|3=}}</code> gives MonTueWedThuFriSatSun
<code>{{short DOW css d|6=|3=|5=|3=}}</code> gives MonTueWedThuFriSatSun
Without CSS support the result is MonTueWedThuFriSatSun

Again, the results are in the index order according to the template content, not in the order of the parameters in the template call. Multiple occurrences are removed.

Comparison of the CSS method and the parameter default methodEdit

Comparing the CSS method and the parameter default method, we see that each of the lines

<span style="display:non{{{3|e}}}">Wed</span>
<span class="hiddenStructur{{{3|e}}}">Wed</span>

in a template T, called with


respectively, give Wed, and the empty string if 3 is left undefined.

Thus the parameter default method has similar functionality, but requires an extra parameter. It has the advantage that the result can be used in expressions for template names, parameter names, parameter values, page names in links, etc.

For comparison, suppose we would use the shorter word "ifu" instead of "HiddenStructure" (note that this would require that class "if" is undefined); then the lines are:

<span style="display:non{{{3|e}}}">Wed</span>
<span class="if{{{3|u}}}">Wed</span>
{{{          if{{{3|u}}}| Wed}}}

In the third method the wikitext in the template is 15 characters shorter than the second method, for each optional item, but each call is 5 characters longer.

In the second method conflicts with other class names have to be avoided, in the third method conflicts with other parameter names. The first method is best in this regard.

Array technique using a small auxiliary templateEdit



using Template:Array, containing {{{{{{index}}}|{{{default|}}}}}}.

In the case of a constant index this can be useful for selection from predetermined options by a small edit. Alternatively index can be an expression depending on a variable, or, if the text is in a template, on a parameter.

Note that if only one template is used, the difference with the method above is that the array data are outside the template: they are provided in the template call.

Example where the text is in another template (with also a default value): Template:Short DOW, containing:

  |default={{{1}}} is not a valid weekday number.


{{Short DOW|3}} gives Wed

{{Short DOW|{{CURRENTDOW}}}} gives Tue

{{Short DOW|7}} gives 7 is not a valid weekday number.

Another version of the two templates could use a term other than "index", e.g. the empty string. However, it should be a term that itself is not used as array index:

{{t Short DOW|3}} using Template:T Short DOW, containing:

  |default={{{1}}} is not a valid weekday number.

(hence using implicit name "1" instead of "index") gives "3 is not a valid weekday number.". The implicit 1=3 is overwritten by "1=Mon", and {{{Mon}}} would be produced; since it is undefined, the default is produced, with in this case a confusing error message: not the input is incorrect, but the template itself.

Method of erasing specification for erasureEdit

This method of creating a data array using templates is not recommended.


A template, the so-called concatenation template, produces the values of all its parameters. It is called by a data template that assigns all data to the parameters, then erases the values of the parameters of which the names are its own parameters, resulting in erasing all values except the desired one. The root template provides the selection of values to be erased: all parameters are specified for erasure from the list to be displayed, but for the desired one the specification for erasure is itself erased.

Language name exampleEdit

{{langname|de}} gives German:

The "root template"

Template:langname containing "{{n en alt|de=de|el=el|en=en|{{{1}}}=}}"

assigns "de" to the variable de, "el" to el, "en" to en, and with the last assignment assigns the empty string to the variable {{{1}}}, that is to say to de. Then it calls the "data template"

Template:n en alt containing "{{concatlang|de=German|el=Greek|en=English|{{{de}}}=|{{{el}}}=|{{{en}}}=}}"

which assigns twice each variable name, the first time with a fixed variable name and the result value, the second time with the variable name that has been passed and an empty value. Thus, the variable el which had received the value "Greek" is assigned back to the empty string since {{{el}}} = "el" ; idem for the variable en, but the nullifying assignment is done with {{{de}}} being empty, so that this assignment does not nullify the first assignment to de, which therefore keeps its "German" value.

Once those parameter assignments has been made, the template calls the "concatenation template"

Template:concatlang containing "{{{de}}}{{{el}}}{{{en}}}"

Since the only non-empty parameter is {{{de}}}, which has kept its "German" value in the previous stage, the end result is "German". QED.

A 24 by 60 arrayEdit

A concatenation template for general use, with unnamed parameters 1 - 1440, is Template:Concat1440.

The root template Template:Array1440 is for a 2D array of size 24 × 60, with index values "00:00" - "23:59". Thus it can also take a time as index value, in particular the result of {{CURRENTTIME}}.

A second set of index values is used in defining the array elements. These index values may or may not be the same as the first set. In the example above they are the same, here they are not, we have the index values 1 - 1440. Thus the template contains 1440 pieces of code of the form "00:04=5". The template is called with two parameters: the array name (i.e. the name of the data template) and the index pair, e.g. "00:04", which can also be seen as a single index.

A small modification fixes the first (part of the) index to 0: Template:Array60.

A matching example data template is Template:Array1440 data demo. The first part contains 1440 pieces of code of the form "5=abc". The second part contains 1440 pieces of code of the form "=". This part can be reused for other data arrays with these index values.

Template:City array demo, containing:

{{concat1440|2=Amsterdam|3=Rotterdam|4=The Hague|12=Amersfoort|17=01|{{{00:01}}}=|{{{00:02}}}=|{{{00:03}}}=|{{{00:04}}}=|{{{00:05}}}=|{{{00:06}}}=|{{{00:07}}}=|{{{00:08}}}=|{{{00:09}}}=

shows that the first part can contain less than 1440 data, while for the second part this is also the case, and this part may contain more than the first part, for convenient expanding of the array by just adding to the first part.


{{array1440|city array demo|00:02}} gives Rotterdam

{{array1440|city array demo|00:03}} gives The Hague

{{array60|city array demo|02}} gives Rotterdam

Example of using array elements as parameters:

{{t2|{{array60|city array demo|01}}|{{array60|city array demo|02}}}} using Template:t2 containing "Parameter 1 is "{{{1}}}", parameter 2 is "{{{2}}}"<noinclude>[[Category:Demo template]]</noinclude>" gives Parameter 1 is "Amsterdam", parameter 2 is "Rotterdam"

Examples of using a template result as (or in) an array index:

{{array60|city array demo|{{x2|1}}}} gives Amersfoort.

{{array60|city array demo|{{array60|city array demo|16}}}} gives Amsterdam.

Plain numbers as index valuesEdit

{{t array|square|3}} using Template:t array (talk, backlinks, edit) containing:


and Template:square (talk, backlinks, edit) containing:

<includeonly>{{#expr:{{{1}}}*{{{1}}}}}</includeonly> <noinclude> Gives the square of a number: ;Usage:<tt>&#123;{square| a}}</tt> ;Example:<tt>&#123;{square|25}}</tt> :Equals: {{square|25}} [[Category:Mathematical templates|{{PAGENAME}}]] </noinclude>

and Template:Concat1440 gives 1



{{t array|square|{{array|square|2}}}} gives Expression error: Unexpected * operator. .

For larger arrays, extend the three lists: k=k, k=data, {{{k }}}=. The fourth list is already available for 1440 index values.

Other exampleEdit

{{t array|array demo|3}} using Template:t array (talk, backlinks, edit) containing:


and Template:array demo (talk, backlinks, edit) containing:


and Template:Concat1440 gives .

An array method using an auxiliary template for each value of the index usedEdit

An array method using an auxiliary template for each value of the index used (which can be reused in other arrays) is as follows (an alternative for "P" is possible, but consistency is desirable):

  • For all values of the index used, Template:Pvalue-of-index is defined, with the content {{{value-of-index }}} (see list for this project and list for the English Wikipedia). Where suitable, indexes of various arrays should be named the same, so that these templates can be reused.
  • An array has the form of a template with the contents
    • {{p{{{1}}}|value-1-of-index =value-of-array-element-1 |value-2-of-index =value-of-array-element-2 |..}}
  • or if index values 1,2,3,.. are used, simply
    • {{p{{{1}}}|value-of-array-element-1 |value-of-array-element-2 |..}}
  • An array element is referred to as {{template name |index }}.

Alternatively the parameter {{{1}}} is replaced by a variable, as in Template:Nsn (name space number). Since for every applicable value of the variable a template has to be created, this is mainly suitable for variables that do not take too many values, and also in the case that the template is only applied in cases where a suitable subset of values applies.

In the case of a parameter {{{1}}} there is no reason to choose the parameter names (before the "=", and in the tag in the corresponding template) differently from the corresponding index values (the template names without the "P"); even the empty string is a valid parameter name.

One has to decide whether a blank space or an underscore is used, they are not distinguished in template names, but they are in parameter names; the templates P{{NAMESPACE}}, and hence the arrays that call them, use a blank space, where applicable, see e.g. Template:PHelp talk (although {{ns:3}} gives User talk with an underscore, it is anyway not possible to have a parameter name depend on a variable, see below). Capitalization has to be consistent for parameter names as well as inside template names, as a P comes in front.

A disadvantage compared with the system mentioned in the previous section (arrays with a template for each element) is that the absence of a data value shows up as {{{index }}}, as opposed to presenting a link to fill in the value. Referring to a defined array element without the auxiliary Template:Pindex being defined, a link to that template does show up, allowing this to be easily fixed.


One application is a template that contains translations of a particular term, where the indexes are the language codes.

Example 1:

Template:Book contains

  • {{p{{{1}}}|de=Buch|en=book|fr=livre|nl=boek|zh-hans=图书|zh-hant=圖書}}

Template:Pfr contains {{{fr}}}, etc.

{{book|fr}} gives livre.

Example 2:

Template:Nsnp (namespace number, with parameter) contains

  • {{P{{{namespace}}}|=0|Talk=1|User=2|User talk=3|Template=10|Template talk=11|Help=12|Help talk=13}}

Template:PUser contains {{{User}}}, etc.

{{nsnp|namespace=User}} gives 2.

{{nsnp|{{NAMESPACE}}}} gives 12.

Compare Template:Nsn (namespace number), which contains

  • {{#switch:{{NAMESPACE}}|=0|Talk=1|User=2|User talk=3|Template=10|Template talk=11|Help=12|Help talk=13|NL Help=116|Overleg help=117}}

{{nsn}} gives 12 (the same).

A 2D array with each row (or column) contained in a templateEdit

A collection of templates can be used as a 2D array: each row is contained in a template, and the template names consist of a common part (which can be considered the name of the 2D array) and a row index.

Thus an array element is referred to by {{2D-array-name index1 |index2 }}

Example: 2D array N

Template:Nlanguage-code contains the word for various languages, expressed in one language.

Template:N de contains

  • {{p{{{1}}}|af=Afrikaans|de=Deutsch|en=English|fr=Französisch|nl=Niederländisch}}

{{n de|fr}} gives Französisch.

The choice of what is put together in one template (a row or a column of a given matrix) can either be based on whether {{n de|fr}} or {{n fr|de}} is a more natural notation for this word, or, as has been done here, on what is more practical in filling the templates with data.

A 2D array contained in a single templateEdit

A 2D array can be contained in a single template if a composite index of the form index1 separator index2 is used.

Thus an array element is referred to by {{2D-array-name |index1 separator index2 }}

The same remarks as above apply for the separator.

Example: Template:Ln contains

  • {{#switch:{{{1}}}|de en=German|de de=Deutsch|de nl=Duits|de fr=Allemand|fr en=French|fr de=Französisch|fr nl=Frans|fr fr=Français}}<noinclude>Small example of [[Help:Localisation|one template containing a whole 2D translations array]]</noinclude>

{{ln|de fr}} gives Allemand.

Arrays of higher dimensionsEdit

An 3D array can have in each template a single element, a 1D array, a 2D array, or the whole 3D array; correspondingly an element is referred to in one of the folowing ways:

  • {{3D-array-name index1 separator1 index2 separator2 index3 }}
  • {{3D-array-name index1 separator index2 |index3 }}
  • {{3D-array-name index1 |index2 separator index3 }}
  • {{3D-array-name |index1 separator1 index2 separator2 index3 }}

An example of a 4D array is at Commons, with elements of the form Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}} ({{{lang}}}), each in a separate template, such as commons:Template:Potd/2005-06-2 (de). Most of the templates on the project are in this array (see list).

Correspondingly there is a 3D array of image names of the form Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}} such as commons:Template:Potd/2005-06-2 and a corresponding set of images of the form [[Image:{{Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}}}}. The latter can also be considered a 3D array, but with the restriction that elements can only be accessed as such from the project itself (the elements can be accessed, and from Wikimedia projects be embedded, e.g.  , but the key to them in terms of the indexes can only be accessed, as the link above shows, but not automatically used).

Other ways of organizing arrays for easy accessEdit

Sections and anchors can be used, if they are systematically named. This only allows separate viewing, not inserting array elements in other texts.


  • Pages for each month with an overview of the 2D sub-array of the 4D array on Commons, together with the corresponding images, in combination with section linking, provide direct access to the overview of the 1D sub-array and the image for a given day, for today [[commons:Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}#{{CURRENTDAY}}]] giving commons:Template:Potd/2022-08#16.

Naming conventions of array templatesEdit

It is not obvious whether a singular or plural name should be used if a single template contains several data, but they are retrieved one at the time, e.g. the template Book above could be called "Template:Translation of "book"" or "Template:Translations of "book"". Similarly Template:Name of talk namespace could also be called Template:Names of talk namespaces.

Since editing a template in general can be considered a little more advanced than just using it by putting a template tag in a page, it can be argued that the naming can best be done from the point of view of people doing the latter, i.e. singular, just like in the case of a template for each array element: {{Translation of "book"|de}} just like the alternative {{Translation of "book" - de}}.

In-page array contentEdit

Instead of having a separate template to define the content of an array, the content of one or more arrays can be put in a template (or, if the array elements are selected based on a variable, it may also be a regular page) between other text.

In this case we have

  • {{pindex |value-1-of-index =value-of-array-element-1 |value-2-of-index =value-of-array-element-2 |..}}

or if index values 1,2,3,.. are used, simply

  • {{pindex |value-of-array-element-1 |value-of-array-element-2 |..}}

where index is an expression in terms of one or more parameters or variables.

Several arrays may use the same index value, to have corresponding selections of array elements, or different ones, e.g. to allow all combinations. An example of the former follows.

Template:Creature demo contains:

A '''{{p{{{1}}}|centaur|griffin|harpy}}''' is a cross between {{p{{{1}}}|a man|a lion|a woman}} and {{p{{{1}}}|a horse|an eagle|a vulture}}.


  • {{creature demo|1}} gives: A centaur is a cross between a man and a horse.
  • {{creature demo|2}} gives: A griffin is a cross between a lion and an eagle.
  • {{creature demo|3}} gives: A harpy is a cross between a woman and a vulture.

Optional textEdit

Due to the blank Template:X0 ("do 0 times"), in each selection an additional option is using index "0", giving no text. This allows putting a text which is optional, 1=on, 0=off.

(Note that a blank page cannot be created directly; first create a non-blank page, e.g. with just one character, then edit the page to blank it. )


Template:Variable text demo 1 contains

  • With a speed of 30 m{{x{{{1}}}|etre}}/s{{x{{{1}}}|econd}} a distance of 1800 m{{x{{{1}}}|etres}} is covered in 60 s{{x{{{1}}}|econds}}.

{{Variable text demo 1|0}} gives

  • With a speed of 30 m/s a distance of 1800 m is covered in 60 s.

{{Variable text demo 1|1}} gives

  • With a speed of 30 metre/second a distance of 1800 metres is covered in 60 seconds.

Example with multiple similar linesEdit

{{variable text demo|1}} gives the long version of a text:

  • With a speed of 30 metre/second a distance of 600 metres is covered in 20 seconds because 600 / 30 = 20.
  • With a speed of 10 metre/second a distance of 500 metres is covered in 50 seconds because 500 / 10 = 50.

while {{variable text demo|0}} gives the short version:

This construction uses the following templates:

  • Template:Variable text demo (see wikitext) - a template that produces a list, with, depending on a parameter ("comprehensiveness toggle"), either long versions of all items, or short versions; it contains all data that is not common to all lines
  • Template:Variable line demo - defines the pattern of a list item, including which parts are only in the full version and which also in the short version; it contains the text that is common to all lines

Comparison of methodsEdit

The method referred to as "array technique using a small auxiliary template" requires one template for each array and just a small one for general use. This makes copying to another project much easier than other methods, especially if no export/import feature is available.

The "method of erasing specification for erasure" requires three templates, one with the data, one depending on the index values used, and a general one.

The other methods require a separate auxiliary template for each value of the index used, or even a template for each element of each array.

See alsoEdit

Links to other help pages

Help contents
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · Wikidata · MediaWiki · Wikibooks · Wikisource · MediaWiki: Manual · Google
Versions of this help page (for other languages see further)
What links here on Meta or from Meta · Wikipedia · MediaWiki
Go · Search · Stop words · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
Tracking changes
Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
Logging in and preferences
Logging in · Preferences · User style
Starting a new page · Advanced editing · Editing FAQ · Edit toolbar · Export · Import · Shortcuts · Edit conflict · Page size
Links · URL · Piped links · Interwiki linking · Footnotes
Style and formatting
Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · List · Table · Sorting · Colors · Images and file uploads
Fixing mistakes
Show preview · Testing · Reverting edits
Advanced functioning
Expansion · Template · Advanced templates · Parser function · Parameter default · Variable · System message · Substitution · Array · Calculation · Transclusion
Special characters · Renaming (moving) a page · Preparing a page for translation · Talk page · Signatures · Sandbox · Legal issues for editors