Help:Advanced templatesEdit This Page

From FamilySearch Wiki

Revision as of 05:58, 13 October 2013 by Parrisl (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Help-content.png This help article will guide you as you edit in the Wiki. See Contributor Help for more help articles.

How to ...
Edit
Basic editing
Simple edits
Editing tool
Advanced editing
Links
Using Rich Editor
Internal links
External links
Advanced linking
Pages
Naming
Moving (renaming)
Deleting
Redirects
Style guide
Formatting
Wiki markup
Wikitext cheatsheet
Collaborate
User page
Talk pages
Updates
Tracking changes
Recent changes
Watchlist
Images
Selecting images
Uploading
Getting approved
Adding to articles
Tables
Advanced tables
Spreadsheets
Categories
Categorization
Advanced
categorization
Templates
Quick guide
Advanced templates
This article covers advanced template techniques, in particular the use of variable template names and parameter names in templates. Readers should be thoroughly familiar with the standard template techniques found in Help:Template. Some techniques described below may be outdated or of limited use; for example, mw:ParserFunctions may be more convenient than some of the branching techniques discussed.

In general, these techniques rely on the recursive (inside-out) processing of templates. When a template is expanded (processed), it translates template code into string values. These are often sent directly to the web browser for display, but they can be treated as code themselves. By enclosing the text strings in double-curly-brackets ( {{}} ), for instance, they can be reprocessed as template names or variable names, producing different results as the string values are changed.

Notes:

  • This document refers to features currently only available in the Mediawiki software starting with version 1.6 and later.
  • The term "variable" has two uses in this document:
    • As noun it means a type of magic word, which is a system-based variable that can be accessed in the same manner as templates (thus {{NAMESPACE}} will return the name of the current namespace, depending on the page).
    • As adjective or adverb, it is used in the general sense, to indicate that something may vary with context (thus a "variable template name" means that the name of the template being called can change according to parameters or variables).
  • Using "subst:" with manual recursion gives a stepwise replacement, useful for analyzing and explaining the working of templates calling other templates.

Contents

Variable templates

In some cases, it may be useful for a template to call different templates depending on a variable or a passed parameter. This allows a type of branching that can be simpler than with mw:ParserFunctions, though they are generally less versatile.

Examples:

Using a variable to choose a template - {{ {{NAMESPACE}} }}
The magic word {{NAMESPACE}} returns the current namespace, like so: {{NAMESPACE}} = Help. The outer brackets then process this result as a template in its own right - in this case Template:Help - which produces

Learn.

Using a template to choose a template - {{{{tctc}} }}
the template {{Tctc}} contains the text "tc". This text is processed by the outer brackets as Template:Tc which contains the word "in".

The extra spaces in the above examples are needed: without them, the pair of inner three braces is taken as those for a parameter. On the page itself it just shows as plain text: {{{{NAMESPACE}}}}.

Using a parameter to choose a template - {{{{{2}}}x|{{{1}}}}}
The second parameter passed becomes part of the template name to which the first parameter is passed. In this case {{2x|{{{1}}}}} would produce {{{1}}}{{{1}}}, {{3x|{{{1}}}}} would produce {{{1}}}{{{1}}}{{{1}}}, and etc. Template:Histogram uses this technique repeatedly in {{histogram|X|4|7|3|2|9}} to produce the following 5 line histogram:
XXXX
XXXXXXX
XXX
XX
XXXXXXXXX

Using parser functions and templates, including those which process strings (see Help:String functions and Category:String templates), a template name can also depend in a more complicated way on parameters and/or variables.

Templates passed as parameters

Templates can be passed as parameters to other templates. This can mean either that the template is evaluated and the result is passed as a parameter or that the template name is passed and evaluated as part of the other template.

Passing a template result - {{3x|{{tc}}}}
Template:3x contains {{{1}}}{{{1}}}{{{1}}}. {{3x|{{tc}}}} first evaluates {{tc}} (which yields the word in), and passes that to template {{3x}}, to give ininin.
Passing a template result recursively - {{3x|{{5x|{{tc}}}}}}
Just as above except {{tc}} (in) is first passed to {{5x}} and the result of that is passed to {{3x}}, to give ininininininininininininininin.


Passing a template name - {{tt|t|V}}
Template:Tt - {{{{{1}}}|a{{{2}}}b{{{2}}}c{{{2}}}d}} - takes the value V (passed as the second parameter) and produces aVbVcVd. This value is then passed to Template:T (which was passed by name as the first parameter), producing "[[Template:aVbVcVd|Template:AVbVcVd]]".

Variable parameter name

A parameter name in a template can be the value of another parameter. This is useful if you want the behavior of a template to change based on the information that is provided to it.

Choosing parameters contextually - {{t pnd|parameter name}}

Template:T p contains {{{capital}}} is the capital of {{{country}}}, with two parameters - "capital" and "country". Template:t pnd containing "{{t p|{{{1}}}=abc}}<noinclude>[[Category:Demo templates|{{PAGENAME}}]]</noinclude>" can be used to select which parameter is used in a particular case. Thus:
This... Produces this
{{t pnd|capital}} abc is the capital of {{{country}}}.
{{t pnd|country}} {{{capital}}} is the capital of abc.
{{t pnd|something else}} {{{capital}}} is the capital of {{{country}}}.

This applies to integer parameters as well, since integer parameters can be specified using the "#=" notation. Passing the integer value N to {{t pnd}} as its parameter will make it look for the Nth unnamed parameter.

Parameter name from another parameter in the same template - {{ppp|p=foo|foo=bar}}
using Template:Ppp, which contains {{{{{{p}}}}}}, the code bar will first set the parameter named "foo" to the value "bar", and then set the parameter named "p" to the value of foo, yielding bar. The order in which the parameters appear in the code does not matter, but the technique cannot be applied multiple times—e.g., using Template:Tvvv, which contains {{{{{{{{{p}}}}}}}}}, {{tvvv|p=foo|foo=bar|bar=biz}} gives {{{bar}}}.

This is e.g. applied in W:Template:Reg polyhedra db, which contains a 2D array in the form {{{{{1}}}|{{{2}}}|1-1=a11|..|m-n=amn}}. The first parameter is the name of a template that provides a particular selection and presentation of a selected row of the array, e.g. W:Template:Reg polyhedron stat table, the second parameter (which is the first parameter of the latter template) specifies the row. The latter templates references element j of the row concerned by a tag of the form {{{{{{1}}}-j}}}

Whilst the same output could also be produced using {#switch:}, this method is less intensive on the server and may help to stay under page limits; see Help:Array.

Branching techniques without ParserFunctions

The parameter default feature was introduced before mw:Extension:ParserFunctions. This led to the development of branching methods through the parameter default mechanism.

If-defined branches - {{{test{{{test|}}}|{{{then}}}}}}
If no value is passed for the parameter test, then {{{test{{{test|}}}|{{{then}}}}}} resolves to {{{test|{{{then}}}}}} and returns a blank entry (since test is defined as blank). If the parameter "test" is assigned the value "boo", however, {{{test{{{test|}}}|{{{then}}}}}} resolves to {{{testboo|{{{then}}}}}}, and so long as no parameter "testboo" exists, then this will return the value of the parameter "then".

See Template:Ifwpc for comparisons.

There was also an array technique using parameter defaults, with the disadvantage that a template using this technique had to be called with, in addition to the normal parameters, a standard parameter definition not reflecting a choice, but necessary to make the template work.

An even older branching technique dates from before the introduction of the parameter default mechanism. It is based on the fact that if in a template call a parameter is assigned a value more than once, the last one counts. This is used in combination with specifying the value of a parameter in a template call, where the name of that parameter depends on a parameter of an outer template. In a call {{a|b=c|{{{d}}}=e}}, template:a uses b=c if b≠{{{d}}} and b=e if b={{{d}}}. See Template:T pdc.

Another old "branching technique" is using a template name depending on the value of a parameter (see above).

Variable variable names

Magic word depending on a template - {{t curr}}
Template:t curr containing "[[:Template:T curr]]" gives the text {{CURRENTTemplate:T day}} without parameters, since it defaults to {{CURRENTDAY}}, but {{t curr|DAYNAME}} gives Template:T curr while {{t curr|MONTHNAME}} gives Template:T curr. Any magic word that begins with "CURRENT" can be accessed this way.
Parser function parameter depending on a template parameter - {{ns:{{{1}}}}}
In Template:Namespace, which contains "[[:Template:Namespace]]", {{namespace|4}} gives "Template:Namespace", because Meta is the name of namespace 4.

External examples