Help:Substitution

Substitution is automatic conversion of wikitext of a page referring to a, variable, or parser function  when the referring page is saved.

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 modifier  after the double opening braces.

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.
 * Stay under the.
 * Analyse and demonstrate the working of templates.
 * 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 and variables. 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).

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.


 * There are exceptions, for example in the case of an undefined parameter with a default value:
 * gives, while gives "Expression error: unrecognised punctuation character "{"", because in the case of substitution of #expr,  is not reduced to 3 and therefore #expr concludes that the expression is not valid.  (See also below.)

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.

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. Even parameters in comments are substituted.
 * 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  ; the latter is not replaced by the default q (see below for a construct that is replaced by p if p is defined, and by q if not).

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 optional substitution below.

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

Stepwise substitution for template names depending on the parameter default mechanism doesn't work as one may expect, this also affects parameter defaults in the left hand side of parser functions, see below for examples.

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, and removes &lt;includeonly&gt; tags.

Predefined templates
In the case of substituting a predefined template, without parameters depending on other templates, the tag is replaced by the result. Please note that 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:


 * , (UTC)
 * 10 March 2005, 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 Image.
 *      </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 does not reduce to 3, so a string function like lc can be applied but not e.g. #expr. For example:
 * gives
 * gives
 * gives the wikitext and rendering Expression error: unrecognised punctuation character "{"
 * substituting a template containing or  gives the same error if p is not assigned a value, but 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.

Derived template names
Substitution of a template whose name depends on another template like  </tt> or a parameter like   </tt>, can only be applied if template B is substituted too, or if the parameter P has a value. Like above, a specified default as in   </tt> is not enough.

Saving e.g.  </tt> is rendered as wikitext, not the desired effect:.

However  </tt> would be replaced by the result of <tt>  </tt> as expected for a substitution in namespace Help.

Summary: Substitution of the whole  works only if all inner y are also substituted.

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. If we also specify a default value q by the code, then with substitution and undefined parameter "subst" this evaluates to the literal code "  " (see below), so regardless of q, never to "subst:". Therefore in the case of substitution parameter "subst" has to be defined anyway. In the case of transclusion and undefined parameter "subst" it evaluates to q. Since inner substitution in only carried out if the outer template is also substituted, the only sensible value of q is the empty string. This is indeed convenient because with non-substituted transclusion we can then dispense with the parameter definition "subst=" in the outer template call.

Thus adding the feature of optional substitution to an existing template X can be done as follows:


 * 1) For each template, parser function, and variable Y used within X, say <tt>{&#123;Y|a|b|c&#125;}</tt>, replace Y by <tt>{&#123;{subst|}&#125;}Y</tt> resulting in <tt>{&#123;{&#123;{subst|}&#125;}Y|a|b|c&#125;}</tt>.
 * 2) For ordinary templates add a parameter <tt>subst={&#123;{subst|}&#125;}</tt> resulting in <tt>{&#123;{&#123;{subst|}&#125;}Y|a|b|c|subst={&#123;{subst|}&#125;}&#125;}</tt> (predefined templates like parser functions and variables don't get the additional parameter).

This allows to use <tt>{&#123;X|d|e|f&#125;)</tt> as is, or <tt>{&#123;subst:X|d|e|f|subst=subst:&#125;)</tt> with recursive substitution, as far as all ordinary templates Y used within X support this technique. Even if they don't support it they are still substituted, but won't propagate this recursive substitution into templates or variables Z used within Y.

Using <tt>{&#123;subst:X|d|e|f&#125;]</tt> without additional parameter <tt>subst=subst:</tt> should be okay, otherwise it also won't work with additional parameter. For some templates substitution breaks their function, and for some predefined templates (see below) and variable REVISIONID it won't work as expected.

As mentioned, also multiple parameters like subst can be used, to control which templates, parser functions, and variables are substituted, see e.g. Template:For.

See also Help:Calculation and.

Partial substitution
Using a template prepared for optional <tt>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 <tt>&lt;noinclude&gt;</tt> parts and <tt>&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 <tt>subst=subst:</tt> is in effect&#58;
 * <tt> </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.

Parameter default considerations
When substituting a template containing (a parameter tag with default) this results in the value of p if it is defined, and otherwise not in q but in  rendered as q. An advantage may be that if the page is transcluded it can still depend on p. A disadvantage is that if this is not needed the wikitext  instead of q clutters the wikitext, and that some operations one may want to apply to q cannot be applied to , for example, if q is a number, a computation with substitution.

In plain text and links the code is cluttering, but otherwise not a problem, for example:
 * , containing
 * <tt> </tt> and called as
 * <tt> </tt> gives the wikitext
 * <tt> </tt>, rendering as.

In other cases there are complications. With substitution a numeric expression containing gives an error message. A string comparison is possible but one should realize that the string contains and not just q if p is undefined.

Instead of one can use



With substitution (subst=subst:) this results in the wikitext q if p is undefined, so this can be used in a numeric expression, e.g.:

which doubles the parameter if is defined, and otherwise returns 2, see.

The alternative code for specifying a default is also useful in the then- and else-part of a parser function if the parameter is 1, 2, or 3, and also in a switch if the parameter name is a possible value of the switch parameter, as workaround for the bug mentioned below.

With Special:ExpandTemplates, gives the wikitext q.

Attempts of direct substitution

 * <tt> </tt> gives
 * Expression error&#58; unrecognised punctuation character "{"
 * An unsubstituted inclusion <tt> </tt> yields.


 * <tt> 1 </tt> gives
 * Expression error&#58; unrecognised punctuation character "{"
 * An unsubstituted inclusion <tt> </tt> yields.


 * Parameter tags with default are compared as strings instead of comparing the default&#58;
 * <tt> 1 </tt> gives 1 like a simple inclusion&#58; ,
 * <tt> 1 </tt> gives 1, but inclusion yields ,
 * <tt> 7 </tt> gives 0 like a simple inclusion&#58; ,
 * <tt> 1 </tt> gives 0, but inclusion yields ,
 * <tt> 1 </tt> gives 1 like an inclusion&#58; ,
 * <tt> 1 </tt> gives 0 like an inclusion&#58;.

Thus <tt> </tt>, a common way to determine if a given parameter is really undefined and not only empty, works also with substitution. Please note that #if: and templates like ifdef don't get this nuance, in the case of #if: an intentional feature for backwards compatibility. For details see advanced templates, in essence now obsolete because #ifeq: unlike #if: solves this problem even if substituted.

URLs
Fullurl and localurl with an undefined parameter cannot be substituted at all, even if a default value is specified. This is a variation of the case of derived template names.


 * <tt> </tt> gives.
 * <tt> </tt> gives.
 * <tt> </tt> gives.
 * <tt> </tt> gives.
 * <tt> </tt> gives http://meta.wikimedia.org/wiki/A.
 * <tt> </tt> gives http://meta.wikimedia.org/wiki/A.

Corrupted default value
If the value of a parameter of a parser function is given in the parser function call as an expression containing an undefined parameter, then the value taken for that undefined parameter is the value of the parameter with that name of the parser function itself, even ignoring a specified default value, if for the parser function a parameter with that name is defined. Compare this with the case without substitution, where this oddity only occurs if no default value was specified. Names and values of parameters of a parser function call are, starting from the first "|", determined as if it were a template. Thus "names" include implicit names 1, 2, ..; an "=" in a parser function parameter, even if treated by the parser function as plain text, is thus interpreted as separator between name and value; also, while "p|q=3" in a switch is normally interpreted as "p=3|q=3", in this connection it is interpreted as "1=p|q=3", etc. The parameter before the first "|" is not affected, it does not have implicit name "0".

Thus the value of undefined parameters, , , and others used with parser functions is corrupted in a predictable way. Instead of the literal string the part between first and second "|" vertical bar or pipe in the parser function is used. In other words the second parameter of the parser function is applied as default of. Similarly, the third parameter of the parser function is applied as default of, etc. Also the value of a named parameter defined in the parser function call is applied as default value for This is not only a theoretical case, a #switch: can have many parameters. See also bug 5678.

For an unsubstituted inclusion this is generally no serious issue: a parameter is supposed to be used in a template, and if it is not assigned a default value, it is supposed to get a value when the template is called. The exact behaviour where that's not the case is less relevant, though it's anyway erroneous.

Unfortunately with substitution the default values are also corrupted following the same pattern, only defined parameters still work. Some parser functions like ns: support only one parameter and are therefore not affected.

Example:


 * " b " gives the wikitext " bb " rendered as "bb".

Workaround:


 * " b " gives the wikitext "ab" rendered as "ab".


 * <tt> error </tt> gives error.
 * <tt> 1 undefined </tt> gives 1 undefined,
 * The <tt>subst:</tt> error is already visible in a preview before the actual substitution.


 * <tt> error </tt> gives.
 * <tt> </tt> gives ,
 * Not equal is correctly concluded, but the else part gives different results.


 * <tt> </tt> gives
 * <tt> </tt> gives
 * Same idea, here the after the 2nd "|" clobbers an undefined.


 * <tt> . </tt> gives oo
 * The "3rd" (actually fourth) parameter clobbers itself, oops instead of ops.


 * <tt> . </tt> gives oops
 * The extraneous "4th" ops corrupted the parameter default k.


 * <tt> . </tt> gives o
 * Without "4th" parameter the specified 4th parameter default k survived.


 * <tt> error </tt> gives error.
 * <tt> </tt> gives.
 * Similar the word error after the 1st vertical bar overwrites <tt> </tt>.


 * <tt> error </tt> gives error.
 * <tt> </tt> gives.
 * The same issue as before for another parser function.


 * <tt> - </tt> gives.
 * <tt> </tt> gives.
 * For what's it worth, it doesn't hit an unnamed parameter <tt></tt>.

Because these examples actually substituted the shown code they won't reflect future fixes automatically. For the actual state of the underlying problem without substitution see ParserFunctions/5678.


 * In the case of #switch: the bug can apply in wild and wonderful ways&#58;
 * <tt> </tt> gives ,
 * <tt> </tt> gives ,
 * <tt> </tt> gives, and
 * <tt> </tt> gives  as expected.


 * However&#58;
 * <tt> a </tt> gives a,
 * <tt> </tt> gives ce.
 * As shown this oddity isn't limited to unnamed parameters etc., it can also hit named parameters, here  and.

A similar problem does not occur with regular templates:
 * <tt> {{subst:t2|error| </tt> gives start-error-middle-{{{1|void}}}-end.

Workarounds
As mentioned above, instead of one can use. This way parameter tags with defaults only appear as strings to be compared, and these do not seem to be affected by the bug.

For a switch inside a template, avoid listed switch index values being equal to names of parameters for which a default is supplied. This can be done by adding some prefix to the expression for the index and to each of its listed values.

If in q and r are numeric expressions, it can be replaced by
 * (suitable if p is short)
 * (suitable if q is short)
 * (suitable if r is short)

(not the same if p is different from 0 and 1), and similarly for #if, #ifexist, and #ifeq.

If r=0 we can simply take

Of course, if the expression is inside another expression we do not need another #expr.

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.