Help:Expansion

From The Scuba Wiki

Jump to: navigation, search
MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins +/-

Expansion of templates, parser functions, variables, and template parameters is done in substitution, and also as the 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:t2demo ( talk edit history links ) containing "start-{{{1}}}-middle-{{{2}}}-end":

  • {{t2demo|a[[b=c|d=e]]f|g}} gives start-ad=ef-middle-g-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:

  • {{t2demo|a[http://b=c d=e]f|g}} gives start-g-middle-{{{2}}}-end
  • {{t2demo|1=a[http://b=c d=e]f|2=g}} gives start-ad=ef-middle-g-end

and, using template:t1demo ( talk edit history links ) containing "start-{{{1}}}-end":

  • {{t1demo|a <span style="color:red">red</span> word}} gives start-{{{1}}}-end
  • {{t1demo|1=a <span style="color:red">red</span> word}} gives start-a red word-end

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:

  • {{t1demo|{{{|1=x}}}}} gives start-1=x-end
  • {{t1demo|{{#if:x|1=x}}}} gives start-1=x-end
  • {{t1demo|{{1=|x}}}} gives start-Template:1=-end
  • {{t1demo|{{1=|x}}=y}} gives start-{{{1}}}-end
  • {{p1=x|{{{|1=x}}}=y}} gives y
  • {{p1=x|{{1=|x}}=y}} gives {{{1=x}}}

Compare:

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:

Contents

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:tc ( talk edit history links ) 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 in
  • {{#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=in
  • {{#switch:q|p=q|r={{tc|s=t}} gives {{#switch:q|p=q|r=in
  • {{#switch:r|p=q|r={{tc|s=t}} gives {{#switch:r|p=q|r=in

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:lb ( talk edit history links ), 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:xpd/msgnw ( talk edit history links )
  • 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 start-p q r-end

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.


Links to other help pages

Help contents
Meta | Wikinews | Wikipedia | Wikiquote | Wiktionary | commons: | mw: | b: | s: | mw:Manual | google
Versions of this help page (for other languages see below)
Meta | Wikinews | Wikipediahttp://en.wikipedia.org/Help:Expansion | Wikiquote | Wiktionary
What links here on Meta or from Meta | Wikipedia | MediaWiki
Reading
Go | Search | Stop words | URL | Namespace | Page name | Section
Backlinks | Link | Piped link | Interwiki link | Redirect | Category | Image page
Logging in and preferences
Logging in | Preferences | User style
Editing
Advanced editing | Editing FAQ | Edit toolbar | Export | Import | Shortcuts
Tracking changes
Recent changes (enhanced) | Related changes | Watching pages | Diff
Page history | Edit summary | User contributions | Minor edit | Patrolled edit
Style & formatting
Wikitext examples | Reference card | HTML in wikitext | List | Table | Sorting | Colors
Special input and output
Inputbox | Special characters | Displaying a formula | Images (uploads) | EasyTimeline
Advanced functioning
Template | Advanced templates | Parser function | ParserFunctions | Parameter default
Variable | Magic word | System message | Substitution | Array | Calculation
Page management
Starting a new page | Renaming (moving) a page | Protecting pages | Deleting a page
Special pages
Talk page | Testing | Sandbox | CentralNotice

Template:-


Personal tools
support the site