Help:Expansion

Expansion of templates, parser functions, variables, and template parameters is done in substitution, and also as first step in page rendering. The result of this first step is shown with Special:ExpandTemplates (see mw:Extension:ExpandTemplates) or with the action=raw&templates=expand parameters ([ like this]). For studying the expansion step this intermediate result is more useful than the rendered page, especially in the case of a table and/or when looking into newlines produced.

Wikitext is parsed with respect to patterns such as, , , , and .. , where a dotted part can contain such structures itself, etc. Pipes, semicolons, and equals signs are, as far as applicable, taken as belonging to the innermost structure in which they occur, and never interpreted as part of an outer structure.

For example, using containing "start--middle--end":
 * gives

However, for the purpose of expansion in the sense of this page, pairs of single brackets and angle brackets are not recognized as creating special structures:
 * gives
 * gives

and, using containing "startend":


 * gives
 * gives

After fully determining which pairs and triples of braces belong together, and which pipes and equals signs belong to the same structure, etc., expansion of the outermost structures starts: for templates, parser functions, variables, and template parameters, the wikitext for the name is expanded. For a template the wikitexts of all parameter names are expanded, and the wikitexts for the names of the formal parameters in the template body, after which it is determined for which parameter values the wikitexts need to be expanded, and for which formal parameters the defaults.

For e.g. parser function #ifexpr, the next step is expanding the condition. Based on that either the wikitext for the then-part or that for the else-part is expanded.

Braces, pipes, semicolons, and equals signs which are produced by expansion are taken as just characters, not parts of structures.

For example, using
 * containing "1="
 * containing ""

we have:


 * gives
 * gives
 * gives
 * gives
 * gives
 * gives

Compare:
 * 1=
 * gives
 * gives

The examples also show that a parameter name "1=x", although impractical, works.

An example of an equals sign in a parameter value not being interpreted in a parameter definition:
 * (using and  containing start--middle--end) gives.

Unbalanced braces

 * If a page has braces pattern ....}}.. the first pair of closing braces is taken to match the opening braces. The second pair of closing braces have therefore no opening braces, so there is no expansion for them.
 * If a page has braces pattern ..{{.... the second pair of opening braces is taken to match the closing braces. The first pair of opening braces have therefore no closing braces, so there is no expansion for them.

More generally, opening braces followed by closing braces (with other text in between) are taken as belonging together. Discounting already matching braces this can be applied recursively. What remains are zero or more pairs of closing braces, followed by zero or more pairs of opening braces.

A page demonstrating the result of excess closing braces and excess opening braces should put the live examples in that order, as is done in the next section.

A page having balanced braces outside nowiki tags has the advantage that the wikitext can be used inside a parameter of a template or parser function. Similarly it allows substitution of the page inside such a parameter. It is not necessary for transclusion of the page inside such a parameter.

Sensitivity to missing or excess braces
A wikitext with various pairs of double braces, e.g. (and what is worse, also a more complicated one) is, of course, very sensitive to missing or excess braces. In the case of missing opening braces of its closing braces are taken as closing braces of #switch, so all remaining wikitext intended to be inside the switch is now outside, and therefore shown independent of a switch index match. The latter also happens in the case of excess closing braces of tc: "}}". The intended closing braces of the switch are taken as plain text.

In the case of missing closing braces in "", i.e. " ... |s=t}}" gives "}}}} ... |s=t}}" (text "..." is preserved)

This raises the point of insertion to the top level (if enough closing braces are used), so that it is not part of the call of a template etc., so not conditional, not used multiple times, etc.

Further examples:
 * gives
 * gives
 * gives


 * |s=t}} gives |s=t}}
 * |s=t}} gives |s=t}}
 * |s=t}} gives |s=t}}


 * {{#switch:p|p=q|r={{tc|s=t}} gives {{#switch:p|p=q|r={{tc|s=t}}
 * {{#switch:q|p=q|r={{tc|s=t}} gives {{#switch:q|p=q|r={{tc|s=t}}
 * {{#switch:r|p=q|r={{tc|s=t}} gives {{#switch:r|p=q|r={{tc|s=t}}

Another method of containing the propagation of any braces error in some wikitext is putting it in a template. For example, if the wikitext with the braces error is "{{", which is the content of {{tim|lb}}, then {{1x|{{#if:|{{lb}}}} ...}} gives "{{1x|{{#if:|{{lb}}}} ...}}", while {{1x|{{#if:| – ...}} gives "{{1x|{{#if:| –  ...}}" (the text "..." is preserved in the first case, but not in the second).

We can apply this method e.g. for a switch, i.e. make each case a template call. If the overall template only contains this switch, based on a parameter, we can dispense with the overall template alltogether and call the subtemplates directly, and they can have one parameter less, except possibly for the default case. If the name of the subtemplate is obtained by concatenation of the name of the overall template and the value of the parameter the template call gets even shorter: {{p|q=r|s=t}} or {{p|r|s=t}} becomes {{pr|s=t}} for supported values of r. In this case there is no mechanism to handle an unsupported r: while with the switch we get nothing or the default, here we would get a call of a non-existing template, giving a red link. Alternatively we could use something like {{p{{#switch:r|.|.|.=r|default}}|s=t}} or {{ {{#switch:r|.|.|.=pr|void}}|s=t}}.

Compare the choice between using switch for an array and using a template for each element.

Advantages of having only a single template:
 * easier to copy (to another wiki, or to make a different version)
 * easier to get an overview of everything; however, this can be obtained on a separate page showing the contents of the templates with msgnw, see e.g. {{tim|xpd/msgnw}}
 * easier to make overall find/replace changes, etc.
 * a common part before or after the switch is possible without duplication
 * in the case of a common part, although it can be made a template, with subtemplates we at least need a template call in each

Advantages of having a template for each case:
 * no propagation of wikitext errors
 * less server load

HTML source
In the process from wikitext to rendering the expanded wikitext is an intermediate result which helps to understand the process. Another intermediate result that can similarly be helpful is the HTML sent to the browser.

Thus we have e.g.:

The step from HTML to rendering depends on CSS: common and personal CSS files referred to in the HTML code, as well as possibly a CSS file specified in the browser.