Help:Substitution

Substitution can be applied to templates, including predefined templates (variables, parser functions, and other "colon functions"). It is done by putting the modifier "subst:" after the double opening braces (in the case of a parser function, without space between "subst:" and "#").

It causes an automatic conversion of wikitext when the referring page is saved.

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.

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

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

Usage of a template through "subst:" does not show up in page histories, backlinks, etc.

Ordinary templates
Example: , containing:

start--middle--end

and called as  gives the wikitext , rendering as start-a-middle--end.

Note that the replacement of a template tag by wikitext does not automatically work recursively: if the template calls another template, that tag is not replaced. One can subsequently add "subst:" again, thus doing manual recursion. This stepwise replacement can be useful for analyzing and explaining the working of templates calling other templates. See e.g. Template talk:Lop. However, there are some complications:
 * If the name of a parameter depends on the default value of another parameter then applying subst does not evaluate that parameter. See

It can also be used to create a page independent of templates, as a way to archive the rendered page and allow a permalink to it. See also 2777, a request for a version of subst that automatically works recursively.

If you want to use a modified version of the resulting wikitext, you have to save the page with the unmodified wikitext first, then edit it, because when applying preview the subst code is not yet replaced in the edit box. In the edit summary of the temporary edit you can put "tmp" to indicate that you have not made an error but are still working on the page.

In the absence of parameters, an alternative that clutters the edit history less is, of course, copying the text from the edit box of the template page: then it can be edited before saving. Another method is using msgnw without saving, and copying the preview result into the edit box.

Applying subst to a template containing four tildes in nowiki tags, the latter are preserved but not applied, i.e., the wikitext of the link to the user name, as well as date and time, are rendered.

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

,    (UTC)

giving: 10 March 2005,   08:23 (UTC)

using gives IN. gives Image.

gives 24.

However:

gives the wikitext, rendered as  (UC is applied to the text "  ", instead of to the resulting string. Thus the result is different from  , giving IN.

stays, rendered as.

gives Expression error: unrecognised punctuation character "{".

Optional substitution
Suppose page B calls C. One can make page B suitable for both inclusion in A and multi-level substitution in A, as follows: use in B, in the call of C, instead of "subst:" a parameter that takes the value "subst:" or "":. Then B can be included in A with or substituted in A with .. . In the case of more levels, C can call D carrying on the value of parameter subst, with, etc. The parameter used in B can be given one of the possible values as default:  or.

See also and.

Substitution based on inclusion
By including and/or excluding information in a template, based on if it's included or not can be used instead of the nowiki/pre hack if the resulted code is meant to be evaluated.

The code  will be displayed as  when the template is not included, ~ when the template is included, and it will be expanded as the active user when the template is subst'd.

Includeonly can be used for templates that are meant to be substed, to define meta-templates and variables that are to be expanded when the template is subst'd, by adding includeonly to the subst:-part.

For example w:Template:Medcab1 containing: * is expanded when called as, e.g. to the wikitext   rendered as


 * 19 12 2005 text

See also Conv-dist.

nowiki/pre
If a template contains tildes or subst-calls within nowiki/pre tags, they will be expanded, but nowiki/pre tags remain, they can be used to contain source as verbatim.

Examples:

, using which refers to, gives in , rendered as "in". The nowiki tags prevent substitution on saving Template:T nws, and prevents showing "in" on the template page, but when substituting Template:T nws, is expanded to "in" anyway.

, and called as, gives the wikitext

Input box
Similar to applying "subst:" to include page A in page B, in the case that B does not exist yet and that A does not have parameters, is using an inputbox to create B, preloading A. A difference is that the wikitext can be edited before saving.