Help:Hacking templates

From Wikiquote

This page is an extension of Help:Template. It concerns template names, variable names, and parameters depending on a variable or parameter.

A template name or a parameter value depending on a parameter or variable

A template name and a parameter value can both depend on a parameter or variable:

This allows branching depending on variables and parameters. However, since no conditions such as {{{1}}}<a can be evaluated, and no substrings can be extracted from strings, etc., this works best if each parameter and variable has only a limited number of possible values. For example {{CURRENTDAYNAME}} has only seven possible values, so all cases can be handled with seven templates; if there are really only two cases, five of the seven could be redirects. On the other hand , {{CURRENTTIME}} has 1440 possible values, so it would be impractical to create a template or even just a redirect for each. In that case the alternative method described below seems the best way, see e.g. Template:Tim.

Having multiple parameters, which each can have only few values, is more flexible than having the same total number of possibilities in a single parameter.

If a template depends on PAGENAME, then the many possible values need not be a problem: only the pagenames of the pages that call the template apply, and these can be limited.

Branching according to {{{1}}}=a or {{{1}}}≠a is possible, see the section #A parameter name depending on a parameter.

Examples of pages that call a template with a name depending on a variable

Arrays with a template for each element

A set of templates can form a kind of associative array. A 1D array has elements which are templates with names of the form array-name index, a 2D array (matrix) has elements which are templates with names of the form array-name index1 separator index2. The indexes can be any text.

For example, on Meta there are templates with names of the form "Lang_name_-_language referred to_(language used )" (in fact, the pattern is an example of a generalized form of the name pattern, with a text, here a closing parenthesis, after the second index) with codes used for the two languages; the contents of each template is the word for a language, expressed in the same or another language, e.g. Template:Lang name - de (fr) contains "Allemand", which is "German" in French. (See list; for diagonal elements such as Template:Lang name - de (de) there are, in addition, copies such as Template:Lang name - de). See also explanation page.

In the case of a 2D array separator and the possible values of index1 and index2 should be chosen such that there is no ambiguity; no separator is needed if at least one index is of fixed length, or e.g. if the first index consists of letters and the second of digits. The software does not need to parse index1 separator index2, but for convenient human parsing a separator such as a blank space may be preferred in some cases where it is not strictly needed.

Elements of a 1D array can e.g. be referred to inside a template with {{array-name {{{index}}}}} using parameter index, or similarly with a variable. Also a page can successively call each array element using {{array-name index}} with varying index.

In the case of a 2D array, a template may have a row index as parameter and produce a list based on that row of the matrix, or similarly for columns. See e.g. Template:List of Languages.

A disadvantage of having a template for each array element is that it is extremely cumbersome to copy a large array to another project, unless a bot is used; a sysop may also apply export and import, if those features are enabled.

An advantage is that the absence of a data value shows up as a link to a non-existing template, allowing an individual data value to be added easily. Depending on how fast the servers are, adding multiple values may be slower, sometimes much slower, than when fewer templates have to be edited.

Another example:

  • See w:en:Template talk:Articlespace: the namespaces are the index values, the no-talk namespaces are the array values.
  • See : the namespaces are the index values, the talk namespaces are the array values.

A 1D array contained in a template

(See also the alternative method below.)

In the following way an array can take the form of a template (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). 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.

Translations

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

Example 1:

Template:Book contains

Template:Pfr contains Template:Pfr, etc.

{{book|fr}} gives Template:Book.

Example 2:

Template:Nsnp (namespace number, with parameter) contains

  • [[:Template:Nsnp]]

Template:PUser contains Template:PUser, etc.

{{nsnp|namespace=User}} gives Template:Nsnp.

{{nsnp|{{NAMESPACE}}}} gives Template:Nsnp.

Compare Template:Nsn (namespace number), which contains

  • [[:Template:Nsn]]

{{nsn}} gives Template:Nsn (the same).

Alternative method

See Help:Array.

The advantage is that unlike the previous method, no separate auxiliary template is needed for each value of the index used. This makes copying to another project much easier, especially if no export/import feature is available.

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

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

  • [[:Template:N de]]

{{n de|fr}} gives Template:N de.

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 template

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

  • [[:Template:Ln]]

{{ln|de fr}} gives Template:Ln.

Arrays of higher dimensions

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 access

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

Examples:

Naming conventions of array templates

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 content

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:

[[:Template:Creature demo]]

Thus:

Optional text

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

Example

Template:Variable text demo 1 contains

  • [[:Template:Variable text demo 1]]

{{Variable text demo 1|0}} gives

{{Variable text demo 1|1}} gives

Example with multiple similar lines

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

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

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

A variable name depending on a parameter

Example:

{{namespace|4}}, using Template:Namespace, containing

[[:Template:Namespace]]

gives:

Template:Namespace

A parameter name depending on a parameter

A parameter name in a template call (before "=") can depend on a parameter.

For example, using Template:Timc with Template:Timc:

*{{t pnd|capital}}
*{{t pnd|country}}
*{{t pnd|something else}}

gives

This applies also for integer parameter names which normally do not require "=", but are now referred to by an expression on the left of "=".

Furthermore, multiple assignments of values to the same parameter in the same template call are allowed and result in the last value being used.

For example, {{t p|country=Belgium|capital=Paris|country=France}} gives "Template:T p".

This can be combined into the following branching technique:

{{tts|first parameter, with fixed name = name of template to be called if the parameter names are not equal | second parameter, with the name depending on parameters and/or variables = name of the template to be called if the parameter names are equal }} with Template:Tts containing {{{{{name of first parameter }}}}} (in this case that name is the empty string).

Thus, while carrying out an action in the case of equality can also be done simply by using a template name equal to the constant (the fixed name above), this technique also allows an action in the case of inequality, without having to cover all alternative values (different from the constant) separately.

Example:

Branching depending on whether a parameter is empty is illustrated with Template:Ut.

Unfortunately there is no control over the result when the parameter is undefined (as opposed to empty):

Example with an extra parameter:

Consider Template:Timc, which calls Template:Timc: If the first parameter is Not Empty then do the task given by the first parameter with the second, third and fourth parameter as its parameters, else do nothing; if the number of parameters of the task is 0, 1, or 2, ignore the superfluous values.

Using Template:Timc:

  • {{ine|death|[[November 2]], [[2004]]|[[Amsterdam]]|}} gives:

"Template:Ine".

This can be useful inside another template, to avoid the texts "Date of death:" and "place of death:" for a living person.

Also, without a separate template Death, with just the more general Template:Timc:

  • {{ine|p2p1p3|[[November 2]], [[2004]]|Date of death:  |, place of death: [[Amsterdam]]}} gives:

"Template:Ine".

This would not be convenient to use in many template tags, but could be used in an infobox in the form

  • {{ine|p2p1p3|{{{1}}}|Date of death:  |, place of death: {{{2}}}}}

We can apply "subst" to remove a "template layer" for server efficiency. It makes no difference for the editors of the pages that include the infobox, and no difference for the editor who wants to understand all inner workings, but it is more cryptic for the editor of the infobox who wants to use template Ine as black box, without being confronted with the internal coding. It gives:

  • {{call|=p2p1p3|{{{1}}}=x0|1={{{1}}}|2=Date of death:  |3=, place of death: {{{2}}}}}

Applying "subst" again is not possible for the general case where the first parameter may or may not be empty. It would have to be done separately for both cases, thus removing the branching capability.

(Alternatively a separate parameter "dead" with one of the values "dead" and "alive" is used, see e.g. w:en:Template:Infobox President with the auxiliary templates w:en:Template:Infobox President/dead and the empty w:en:Template:Infobox President/alive. A disadvantage is the extra parameter: it has to be specified that a president is alive, it is not sufficient to leave the date of death empty, or that a president is dead, even if a date of death has been specified. An advantage is that backlinks are available, providing not only a list of dead, but also a list of alive presidents for which the infobox is used.)

Similarly, where a table row dealing with a parameter is removed if the parameter is empty:

w:en:Template:If defined call1 calls w:en:Template:Template_call1 with 1={{{2}}} followed by 1{{{1}}}=void; if {{{1}}} is empty then this overwrites the value of 1, so 1=void, otherwise 1={{{2}}}, giving an empty result or {{{{{2}}}|{{{3}}}}}. {{{3}}} is typically the same as {{{1}}} (at least that is the case in all five cases in w:en:Template:Infobox CVG). It avoids e.g. the text "Designer:" if the parameter {{{designer}}} is empty.

More generally, using Template:Timc:

Note that {{NAMESPACE}} produces underscores for spaces, which are not considered equal by the template:

A parameter name in a parameter tag can now depend on a parameter, see Template:T pnd1 and its talk page.

Also a parameter name in a template call can now depend on a variable: {{ns:2}} gives User and {{PUser|User=abc}} gives Template:PUser, hence {{PUser|{{ns:2}}=abc}} gives Template:PUser

Using "subst:" with manual recursion gives a stepwise replacement, useful for analyzing and explaining the working of templates calling other templates. See e.g. Template talk:Lop.

Including the content of a template in a template or variable name or parameter value

It is now possible to use a template with a name dependent on the content of another template. For example: {{{{tctc}} }}, using Template:Timc and Template:Timc, gives "{{Template:Tctc }}". If there are no parameters, a space is needed between e.g. the two pairs of closing braces.

It is now possible (since about 19 October 2005) to use a variable with a name dependent on the content of a template.

For example: {{CURRENT{{t day}}}} using Template:Timc gives the text {{CURRENTTemplate:T day}}, which is indeed the value of {{CURRENTDAY}}

It is now possible (since about 19 October 2005) to include the content of another template in a parameter value

Examples:

Thus it is now also possible to use a data array as parameter of a template, e.g.:

{{hist5a|x|t data}} using

Template:Timcnl and

Template:Timc gives:

Template:Hist5a

Template producing the name of the talk namespace of the calling page

In English language projects the name of a talk namespace is usually simply "{{NAMESPACE}} talk", here giving Help talk. With this a link can be produced: [[{{NAMESPACE}} talk:{{PAGENAME}}]] giving Help talk:Hacking templates

Also one could include a todo page in the corresponding talk namespace associated with each no-talk page; from the no-talk namespace it can be done with {{{{NAMESPACE}} talk:{{PAGENAME}}/todo}} which gives Help talk:Hacking templates/todo (a link to a non-existing template page or an inclusion of the content). From the no-talk namespace it can be done with {{{{NAMESPACE}}:{{PAGENAME}}/todo}}.

Using Template:Name of talk namespace (after completing the list of namespaces) with the corresponding auxiliary templates like Template:PUser (for general use of arrays with the namespace as index), the linking can be done in all languages:

[[{{Name of talk namespace}}:{{PAGENAME}}/todo]] gives [[Template:Name of talk namespace:Hacking templates/todo]].

Alternatively, a separate template is created for each namespace just for this purpose, see [1].

However, these methods work for linking but not for including. One can use {{{{NAMESPACE}}:{{PAGENAME}}/todo}} but if applied in the no-talk and the talk namespace, this gives two todo pages unless a redirect is created.

Therefore, in addition two new variables are being prepared for a new version of the software. When that works one can use, both from the non-talk page and the talk page:

  • {{{{NAMESPACE_TALK}}:{{PAGENAME}}/to_do}}

Test whether they work already:

See also Bug 531.

Illustration of the possibilities of template calls and links

[[aTemplate:T complexb|pTemplate:T complexq]]

is the result of [[a{{t complex|c=d|g=h|k=m}}b|p{{t complex|c=d|g=h|k=n}}q]]

where Template:T complex contains:

[[:Template:T complex]]

and Template:T aHelpbdd contains:

[[:Template:T aHelpbdd]]

[[w:en:{{n en|de}}]] gives [[w:en:Template:N en]].

[[{{n en|de}} TV coverage of Wikipedia]] gives [[Template:N en TV coverage of Wikipedia]].

This page can now also be included based on the code "de" using {{:{{n en|de}} TV coverage of Wikipedia}}.

See also