Help:Substitution

Substitution is automatic conversion of wikitext of a page when the page is saved, in the case that the wikitext refers to one or more ,  variables, or parser functions.

In the case of template substitution the template call is replaced by the template content with substitution of the parameters. Thus a template is used as macro and the page is macro expanded when the page is saved rather than, as usually happens, when the page is viewed.

In the case of substitution of a variable or parser function the reference to it is replaced by the resulting value.

Substitution is done by putting the   after the double opening braces without intervening spaces likle in the example:  .

The result (in the form of the difference with the saved wikitext) can be seen before (or without) saving by pressing "Show changes". However, if the text covers more than one paragraph this diff page is not very suitable for copying the result (e.g. for stepwise substitution without saving every step), because of plus signs in the margin.

Applications
Substitution of a template: Substitution of a time-dependent variable: Substitution of a page-dependent variable:
 * Make a rendered page independent of the template:
 * The rendered page does not change when the template is edited.
 * The page can be copied to another project without copying the template.
 * Make page rendering easier and therefore faster for the server.
 * Analyse and demonstrate the working of templates. However, in some cases substitution works differently.
 * Make the correspondence between wikitext and rendered page easier to understand (this may apply, the opposite may also apply).
 * Make a rendered page independent of the time.
 * Make a rendered page independent of renaming of the page and of copying the wikitext to another page (the opposite applies if the variable PAGENAME is used in a no-include part of the page to include the page itself).

Some MediaWiki extensions have the restriction that if they are used in a template with parameters, they only work if the template is substituted. This applies for example in the case of a template parameter inside an in-page query in Semantic MediaWiki.

Overview
For the discussion of substitution, an "ordinary template" is any page included by {&#123;subst:pagename &#160; &#125;} for pages in the template namespace or {&#123;subst:fullpagename &#160; &#125;} for pages in other namespaces. It's also possible to substitute variables and parser functions collectively known as "predefined templates".

Substitution is a separate process before expansion of non-substituted templates, parser functions, variables. and parameters. Substitutions in the expression for the name of the template or parser function, in the parameter definitions of the substituted template or parser function and in the body of the substituted template are done first. Non-substituted expansion is done later, so at the time of the substitution these expressions with pairs of double braces are treated as plain text. Thus during substitution there can be a parameter name with braces in the call of the substituted template (e.g. ad ) that matches a parameter with the same name in the template body.

If one attempts to apply substitution to a non-existing template etc. there is no substitution, the "subst:" prefix is kept in the wikitext.

After the substitution process, expansion of templates etc. and other processing of the resulting wikitext works as usual. However, this cannot undo a mismatch of parameter names during substitution (see also the second header "Partial substitution" below).

Substitution is only possible if the following has been fully evaluated:
 * the name of the template, parser function or variable
 * in the case of a template: the parameter names in the template call and in the template itself
 * in the case of #if, #ifexpr, #ifexist, and #iferror, the parameter after the colon
 * in the case of #ifeq, the parameter after the colon and the next one
 * in the case of #switch, the parameter after the colon and the expressions to the left of the equals signs

Since, as said, substitution is done before other expansion, the abovementioned required evaluation has not taken place if the expressions involve non-substituted expansion.

Also in the case of other parser functions than mentioned, a not fully evaluated parameter after the colon causes the parser function to be applied to the wikitext with braces and not the expanded wikitext, affecting the result.

Examples:
 * using, does not do substitution, because Help:L k is not an existing page, although Help:Lk is rendered as Help:Lk. Thus the resulting wikitext is the same as the original wikitext and rendered as.
 * yes using, gives the wikitext "yes", while yes gives the wikitext "no", and is rendered as "".

In principle the wikitext resulting from full substitution is, immediately after that, rendered the same as the wikitext with ordinary inclusion.

Note however that on substitution of a parser function, an undefined parameter with default, used in a parameter value, is not replaced by the default, and is for example not allowed in the numerical expression evaluated on substitution of #expr:
 * gives, while gives "Expression error: unrecognised punctuation character "{""

On substitution of a template that contains this, is either replaced by the value of  or by 3, so then there is no complication.

using is rendered as ;  gives the wikitext a rendered the same as the wikitext, and on the next edit/save changed into start-a -middle--end;  gives the wikitext , rendered as. This is because, both without substitution and in the case of full substitution, the pipe characters in template calls, excluding those inside inner template calls, template parameters, links, and image tags, determine the separation of parameter definitions from each other and from the template name. This separation does not depend on possible extra pipe characters in the expanded form of the template name and parameter definitions. However, if after substitution of an inner template the pipe character is in the outer template call it is one like any other and plays its part in determining the separation. In other words, parsing is done first once for substitution, and then once for rendering, but in both cases not an extra time in between. In the case of substitution of the inner template only, two subsequent parsings are effective.

When substituting a template containing (a parameter tag with default) this results in the value of p if it is defined, and otherwise in q. For example, using,  gives the wikitext 2.

If a page substitutes itself (e.g. in the noinclude-part of a template page) it substitutes the old version.

Usage considerations
As mentioned, a change of an ordinary template after substitution does not affect the page in which it was substituted, and a substituted variable depending on time no longer depends on time, etc. However, a substitution of e.g. does not affect rendering at all.

The relationship between wikitext of a page and its rendering can become easier to understand after substitution, because one has all wikitext together, and parameter substitutions have been performed.

It can also become more complex. Separately focusing on understanding a template call and understanding the template content can be easier. Wikitext after substitution is often more complex than when the required wikitext would have been written directly.

Unlike a template call (if one knows about templates), wikitext after substitution does not show how one can produce a similar result. The wikitext can be long and complicated, and therefore cumbersome to write directly, or it can be simple, e.g. a number resulting from a computation, but cumbersome to find directly. One may think that the wikitext is what one is supposed to write and find directly to get the result, even in cases where that would be very impractical.

In such cases documentation of the template call is useful. Just like in computer programming we change the source code and/or the data to produce new results, and we do not directly change the object file, here we would change the template calls and/or the templates, instead of changing the wikitext resulting from substitution directly.

Ordinary templates
In the case of substituting an ordinary template, the template tag is replaced by the wikitext of the template, with the parameter values substituted for the parameters.
 * Example&#58;, containing
 *  start--middle--end 
 * and called as  a  (see tc) gives the wikitext:
 *  start-a-middle--end , rendering as
 * start-a-middle--end.

Substitution removes the noinclude parts and the includeonly tags.

Parameters:
 * A substitution with p=r replaces and  by r; this includes the cases that r is of the form  or.
 * A substitution with undefined p preserves and replaces  by the default q.

With "subst:" the replacement of a template tag by wikitext does not work recursively. For full recursive substitution use Special:ExpandTemplates. See also substall, and multilevel substitution below.

Stepwise substitution of templates including other templates including more templates etc. can be useful for analyzing and documenting the behaviour of complex templates, for an example see m:Template talk:Lop.

However, after a substitution a page may render differently, e.g. if templates produce braces, pipes, and/or equals signs which after substitution determine expansion of other templates, but without substitution are treated as plain text.

In the absence of parameters, template substitution can be compared with copying the wikitext, or the rendering of a previewed or saved {&#123; &#160; msgnw:pagename &#160; &#125;} inclusion. However, template substitution excludes &lt;noinclude&gt; parts, removes &lt;includeonly&gt; tags, and replaces undefined parameters with defaults by the defaults.

Predefined templates
In the case of substituting a predefined template, without parameters depending on other templates, the tag is replaced by the result.

Note: subst: has to be added directly in front of the predefined template name without intervening spaces.

Applying subst to a variable works like applying it to a template. E.g. a timestamp:


 * -- T UTC
 * 2005-03-10 T 08:23 UTC

In the case of substituting a predefined template with a parameter depending on another template, that has to be substituted too, with a separate subst: modifier, otherwise the result is undefined.


 *  </tt> gives IN, the same wikitext as  is expanded to; UC: is applied to the output "in" of Tc.
 *  </tt> gives File.
 *      </tt> gives wikitext      rendered as.
 * gives the wikitext startHelpend (see t1)
 * gives the wikitext start12end
 * gives the wikitext startABCDEFend
 * gives the wikitext 1331
 * gives the wikitext ABCABCABC
 * gives the wikitext 1e-05 (see LC:)
 * gives (at the time of writing) the wikitext 30
 * gives (at the time of writing) the wikitext THURSDAY

However:
 * 1)  </tt> gives the wikitext   </tt> rendered as.
 * 2)  </tt> stays , rendered as  (see ns:).

As mentioned before, on substitution, all calls without substitution of templates, variables, and parser functions are treated as plain text. As a result substitution of the outer x:</tt> in a nested often is only suitable if all inner y:</tt> are also substituted.

In the case of substitution of a predefined template, if the expression for one of its parameters contains with undefined p, this code reduces to 3. However, on the page itself, is treated as such, not as 3.

Examples:
 * gives
 * gives
 * gives Expression error: Unrecognised punctuation character "{"
 * substituting a template containing or  gives 6 if p is not assigned a value, and twice the number p if it is assigned a value.

Compare:
 * 2* gives 2*
 * 2* gives 2*
 * gives the wikitext 2* rendered as 2*

and also (from above):
 *  </tt> gives IN, just like  does; UC is applied to the output "in" of Tc.
 *  </tt> gives the wikitext   </tt> rendered as.

In the substitution of UC, the inclusion tag is treated as string just like.

Partial substitution
Inside an ordinary template one can apply substitution to an ordinary template call containing a parameter, to replace it by the direct wikitext containing the parameter. It amounts to automatically merging the two templates (creating a "composite template" like a composite function). It is not possible if the inner and/or outer template is predefined. (However, manually merging e.g. a call of #expr inside another one is useful for increasing the accuracy of the result by avoiding intermediate rounding to 12 digits.)

This way one can dispense with the optional substitution technique described below, and apply substitution of the resulting outer template by simply using "subst:" (unless there are more nesting levels).

Example:
 * }} gives the wikitext start--end, just that of , without noinclude parts and includeonly tags
 * ab gives the wikitext start-ab-end

Examples with double substitution:
 * gives the wikitext start--endstart--endstart--endstart--end
 * gives the wikitext start--endstart--endstart--end

Multilevel substitution
When substituting a template it may be desirable to carry out a substitution inside the template too. This requires "subst:" in the template. There are two methods to prevent premature substitution (i.e., when the template is saved): the code "subst:" can be the possible value of an expression containing a parameter, or includeonly tags can be used (see below).

With the first-mentioned method (the code "subst:" can be the possible value of an expression) we can:
 * apply multilevel transclusion by making the expression the empty string;
 * apply one-level substitution by substituting the outer template and making the expression the empty string;
 * apply two-level substitution by substituting the outer template and making the expression evaluate to "subst:";
 * apply multi-level substitution by substituting the outer template, making the expression evaluate to "subst:", and calling the inner template with parameters controlling further inner substitutions in the same way; these parameters may depend on the substitution parameter controlling the substitution of the inner template, since if that is not substituted, inner substitutions within that template are not possible.

Similarly, if there are multiple templates, parser functions, and/or variables in the inner template(s) we can control substitution of all, either independently by using different parameters, or with some or all using the same parameter.

The simplest code to control whether we get the empty string of "subst:" is using a parameter which is assigned either value, for example with the name "subst", possibly with a postfix, for independent control of multiple substitutions. We can also specify a default, either the empty string or "subst:".

For example, if template T uses parameter subst1:
 * with the empty string as default, T calls inner templates and parser functions prefixing their names with ; for calling T we can use:
 * .. (no substitution)
 * .. (multilevel substitution)
 * with default "subst:", T calls inner templates and parser functions prefixing their names with ; for calling T we can use
 * .. (no substitution)
 * .. (multilevel substitution)

To transfer the choice of substituting or not to templates and parser functions called inside the inner templates of T, we can add to the call of these inner templates something of the form subst2= or subst2= , respectively (parser functions and variables don't get the additional parameter).

See also Help:Calculation and.

Partial substitution
Using a template prepared for optional subst=subst:</tt> only with ordinary substitution, without specifying parameter values, allows to insert its code into another template, like copy and paste, but all &lt;noinclude&gt;</tt> parts and &lt;includeonly&gt;</tt> keywords automatically stripped. Executing inserted code instead of calling it may be more efficient for the server.

A typical example for this technique is expanding, within another template, a template used as test expression in a #switch: like : parameter tag case 0 etc. parameter tag  case 0 etc. parameter tag  case 0 etc. something&#125;}. This suggests that substitution is prevented by discarding "subst:" on the page itself, but actually substitution is prevented because the subst-syntax is disturbed by the tags.
 * 1) Development code:

It doesn't substitute "something" at the time of the creation of the relevant template, but has the desired effect when the template is substituted. Please note that this works as expected only for substitution, not for normal (unsubstituted) inclusion. For examples see "preload" in Help:Inputbox and "substitution" in Help:Variable.

Creating a page which applies substitution on the next save
See Help:Recursive conversion of wikitext.

Forced substitution
Some templates deliberately refuse to work without substitution, for an example see Conv-dist. This technique is essential for templates like prod producing some kind of timestamp, e.g. adding pages to dated categories.


 * The following code in any template T outputs a warning unless recursive substitution with subst=subst:</tt> is in effect&#58;
 *  </tt>.
 * Output for {&#123;T&#125;} or {&#123;subst:T&#125;}&#58; Warning,
 * output for {&#123;T|subst=subst:&#125;}&#58; ,
 * output for {&#123;subst:T|subst=subst:&#125;}&#58; nothing (no remaining wikitext).


 * This is a rare case where replacing ifdef by #if: doesn't work directly.

Substitution of part of the parameters
Let template A use parameters 1 and 2. Consider creating a template B with one parameter 1, corresponding to A, with a given value q of parameter 2. Compare and. They look the same on the template page, see e.g., but the first does not work because is treated as text, not as parameter.

However, with substitution (using "subst:" or Special:Expandtemplates) the resulting wikitext is the same, without distinction between a text and a parameter, it is a parameter anyway, so "1=" is not needed.

If A contains e.g. #expr with an expression containing both parameters the same applies, except that we can only substitute the highest level (A), not the parser function, so we cannot use Special:Expandtemplates.

In general, substituting a parameter and applying a template or parser function sometimes gives the same result as substituting the template or parser function with the triple-braced parameter code and then substituting the parameter.

Without defaults (all rendered the same in one-step substitution as without substitution):

Examples with equality:
 * A template containing pqr substituted with 1=u, 2=v gives puqvr; substituted with 2=v it gives pqvr, which itself, substituted with 1=u gives also puqvr.
 * Two-level substitution of a template containing with 3=u, 4=v gives up; substituted with 4=v it gives p, which itself, substituted with 3=u gives up.

Examples without equality:
 * Two-level substitution of a template containing with 3=, 4=v gives the empty string; substituted with 4=v it gives vp, which itself, substituted with 3=u remains vp.
 * Two-level substitution of a template containing with 1=u, 2=v gives up; substituted with 2=v it gives pp (the bug), which itself, substituted with 3=u, gives upp.
 * Two-level substitution of a template containing with 1=7, 2=8 gives 56; substituted with 2=8 it gives &lt;strong class="error">Expression error: Unrecognised punctuation character "{"&lt;/strong>, which itself, substituted with 1=7, remains the same.

Thus without equality we may or may not get an error message.

One example shows that substitution of one parameter can be affected by the bug mentioned above. However, we can then replace e.g. by  and do full substitution, except that substvoid is undefined, preventing the bug. The result works already correctly with transclusion. Subsequently it can be substituted with substvoid=subst: so that we get the plain.

With defaults:

Rendered the same as without substitution:
 * Two-level substitution of a template containing pqr with 2=v gives pqvr.
 * Two-level substitution of a template containing with 4=v gives p.

Not rendered the same as without substitution:
 * Two-level substitution of a template containing with 4=v gives vp.
 * Two-level substitution of a template containing with 2=v gives pp (the bug).
 * Two-level substitution of a template containing with 2=8 gives &lt;strong class="error">Expression error: Unrecognised punctuation character "{"&lt;/strong>.

After substitution with the parameter definition:
 * vp gives vp.
 * p gives pp (the bug).
 * gives &lt;strong class="error">Expression error: Unrecognised punctuation character "{"&lt;/strong>.

Rewritten:
 * gives the empty string.
 * p gives dp.
 * gives 48.

Documenting substitution
Usage of a template through  does not automatically show up in page histories. Therefore providing the line of wikitext containing "subst:" in the edit summary is especially useful.

Also pages with a substituted template do not show up in backlinks, and the template does not appear in the list of transcluded templates on the edit page. The template could add pages to a category to track substitutions, but listing this category on a page may clutter the list of content-based categories the page is in. Also, comments outside noinclude tags are included in the wikitext. Thus a comment can be used to mention the template. It can even contain the values of the parameters, because substitution of parameters works even in comments.