From Citroen SM Wiki

Template:H:h 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 Template:Tim containing "start-{{{1}}}-middle-{{{2}}}-end":

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:

and, using Template:Tim containing "start{{{1}}}end":

  • {{t1demo|a <span style="color:red">red</span> word}} gives Template:T1demo
  • {{t1demo|1=a <span style="color:red">red</span> word}} gives Template:T1demo

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

we have:


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:

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. {{#switch:r|p=q|r={{tc}}|s=t}} (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 Template:Tim 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: "{{tc}}}}". The intended closing braces of the switch are taken as plain text.

In the case of missing closing braces in "{{tc}}", i.e. "{{tc", the intended closing braces of the switch are taken as closing braces of Template:Tc (or more generally, as closing braces of the highest level under switch), so the switch does not have closing braces and is therefore not expanded. The latter also happens in the case of excess opening braces of tc: "{{{{tc}}".

In the case of missing closing braces or excess opening braces somewhere on a page, and also missing opening braces or excess closing braces further on on the same page, all text in between is considered part of the template call or parser function call etc., so it may not be shown, shown multiple times, etc., e.g.:

  • "{{#switch:p|p=q|r={{tc|s=t}} ... {{#switch:p|p=q|r=tc}}|s=t}}" gives "q" (seemingly unrelated text "..." disappears)

MediaWiki does not seem to have any facility to see which braces match, and a simple braces error in a wikitext with many braces can be cumbersome to find. Therefore techniques to keep things manageable are useful.

To avoid that a possible braces error in a part of the wikitext of a page affects the expansion of a later part one can temporarily add excess closing braces after the part with the possible errors, at a point in the wikitext that is supposed to be on the top level (outside braces):

  • "{{#switch:p|p=q|r={{tc|s=t}} }}}}}} ... {{#switch:p|p=q|r=tc}}|s=t}}" gives "q}}}} ... q|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:

  • {{#switch:p|p=q|r={{tc}}|s=t}} gives q
  • {{#switch:q|p=q|r={{tc}}|s=t}} gives
  • {{#switch:r|p=q|r={{tc}}|s=t}} gives Template:Tc
  • {{#switch:p|p=q|r=tc}}|s=t}} gives q|s=t}}
  • {{#switch:q|p=q|r=tc}}|s=t}} gives |s=t}}
  • {{#switch:r|p=q|r=tc}}|s=t}} gives tc|s=t}}
  • {{#switch:p|p=q|r={{tc|s=t}} gives {{#switch:p|p=q|r=Template:Tc
  • {{#switch:q|p=q|r={{tc|s=t}} gives {{#switch:q|p=q|r=Template:Tc
  • {{#switch:r|p=q|r={{tc|s=t}} gives {{#switch:r|p=q|r=Template:Tc

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 Template:Tim, then {{1x|{{#if:|{{lb}}}} ...}} gives "Template:1x", while {{1x|{{#if:|{{}} ...}} gives "{{1x|" (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. Template:Tim
  • 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.:

wikitext {{t1demo|p ''q'' r}}
expanded wikitext startp ''q'' rend
HTML startp <i>q</i> rend
rendering Template:T1demo

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. Template:H:f