Help:Substitution: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
→‎Multilevel substitution: If the name of the inner template, parser function or variable depends on a parameter without default, then we can simply put <code><nowiki>safesubst:</nowiki></code>, be
m Reverted changes by 196.117.197.39 (talk) to last version by Saederup92
Tag: Rollback
 
(118 intermediate revisions by 80 users not shown)
Line 1: Line 1:
{{MovedToMediaWiki}}
{{h:h}}[[Category:Editor handbook]]

'''Substitution''' is [[Help:Automatic conversion of wikitext|automatic conversion of wikitext]] of a page when the page is saved, in the case that the wikitext refers to one or more {{mlm|Help:Template||templates}}, [[Help:variable|variables]], or [[Help:parser function|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 [[wikt:en:macro|macro]] and the page is [[wikt:en:macro expansion|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 {{ml|Help:Magic_words|Template_modifiers|modifier}} '''<code>subst:</code>''' or '''<code>safesubst:</code>''' after the double opening braces without intervening spaces like in the examples: '''<code><nowiki>{{subst:FULLPAGENAME}}</nowiki></code>''' and '''<code><nowiki>{{safesubst:FULLPAGENAME}}</nowiki></code>'''. The code <code>safesubst:</code> is useful in multilevel substitution, see below.

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:
*Make a [[w:render|render]]ed page independent of the template:
**The rendered page does not change when the template is edited.
**The page can be copied to another [[MediaWiki]] [[wiki]] 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).
Substitution of a time-dependent variable:
*Make a rendered page independent of the time.
Substitution of a page-dependent variable:
*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 [[Help:substitution|substitution]], an ''"ordinary template"'' is any page included by {&#123;subst:''pagename''<small>&#160;</small>&#125;} for pages in the template namespace or {&#123;subst:''fullpagename''<small>&#160;</small>&#125;} for pages in other namespaces. It's also possible to substitute [[Help:variable|variable]]s and [[Help:parser function|parser function]]s collectively known as ''"predefined templates"''.

Substitution is a separate process before [[help:expansion|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. <nowiki>a{{bc}}d</nowiki>) 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:
*{{subst:Help:L<nowiki>{{</nowiki>tc}}k}} using {{tim|tc}}, does not do substitution, because Help:L<nowiki>{{</nowiki>tc}}k is not an existing page, although <nowiki>Help:L{{tc}}k</nowiki> is rendered as Help:L{{tc}}k. Thus the resulting wikitext is the same as the original wikitext and rendered as {{subst:Help:L{{tc}}k}}.
*<nowiki>{{subst:#if:{{x0|abc}}|yes|no}}</nowiki> using {{tim|x0}}, gives the wikitext "yes", while <nowiki>{{subst:#if:{{subst:x0|abc}}|yes|no}} gives the wikitext "no", and {{#if:{{x0|abc}}|yes|no}}</nowiki> is rendered as "{{#if:{{x0|abc}}|yes|no}}".

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 [[Help:Parameter default|default]], and is for example not allowed in the numerical expression evaluated on substitution of #expr:
:*<nowiki>{{#expr:2*{{{p|3}}}}}</nowiki> gives {{#expr:2*{{{p|3}}}}}, while <nowiki>{{subst:#expr:2*{{{p|3}}}}}</nowiki> gives "Expression error: unrecognised punctuation character "{""
On substitution of a template that contains this, <nowiki>{{{p|3}}}</nowiki> is either replaced by the value of {{{p}}} or by 3, so then there is no complication.

<nowiki>{{ {{t6}} }}</nowiki> using {{tcw|t6}} is rendered as {{ {{t6}} }}; <nowiki>{{subst:{{subst:t6}} }}</nowiki> gives the wikitext <nowiki>{{subst:t2demo|a }}</nowiki> rendered the same as the wikitext, and on the next edit/save changed into <!--t2-->start-a -middle-{{{2}}}-end; <nowiki>{{ {{subst:t6}} }}</nowiki> gives the wikitext <nowiki>{{ t2demo|a }}</nowiki>, rendered as {{ t2demo|a }}. 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 <nowiki>{{{p|q}}}</nowiki> (a parameter tag with default) this results in the value of p if it is defined, and otherwise in q. For example, using {{timc|t pd}}, <nowiki>{{subst:t pd}}</nowiki> 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. <nowiki>{{#expr:2*3}}</nowiki> 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. When studying the wikitext of a page one may think that this 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 [[w:source code|source code]] and/or the data to produce new results, and we do not directly change the [[w:object file|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; {{tim|t2}}, containing
::<tt><nowiki>start-{{{1}}}-middle-{{{2}}}-end</nowiki></tt>
: and called as '''<tt><nowiki>{{subst:t2|[[a]]|{{tc}}}}</nowiki></tt>''' (see {{tl|tc}}) gives the wikitext:
::<tt><nowiki>start-[[a]]-middle-{{tc}}-end</nowiki></tt>, rendering as
::start-[[a]]-middle-{{tc}}-end.

Substitution removes the noinclude parts and the includeonly tags.

Parameters:
*A substitution with p=r replaces {{{p}}} and <nowiki>{{{p|q}}}</nowiki> by r; this includes the cases that r is of the form {{{s}}} or <nowiki>{{{s|t}}}</nowiki>.
*A substitution with undefined p preserves {{{p}}} and replaces <nowiki>{{{p|q}}}</nowiki> by the [[Help:Parameter default|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 [[mediazilla:2777|substall]], and [[#Multilevel substitution|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;<small>&#160;</small><tt>[[Help:Magic words|msgnw:]]</tt>''pagename''<small>&#160;</small>&#125;} inclusion. However, template substitution excludes <tt>&lt;noinclude&gt;</tt> parts, removes <tt>&lt;includeonly&gt;</tt> 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''': <tt>subst:</tt> 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:

;<nowiki>{{subst: CURRENTYEAR}}-{{subst:CURRENTMONTH}}-{{subst:CURRENTDAY}} T {{subst:CURRENTTIME}} [[w:UTC|]]</nowiki>

may give the wikitext

:<nowiki>2010-04-10 T 06:30 [[w:UTC|UTC]]</nowiki>
rendered as
:2010-04-10 T 06:30 [[w:UTC|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 <tt>subst:</tt> modifier, otherwise the result is undefined.

*<tt><nowiki>{{subst:UC:{{subst:tc}}}}</nowiki></tt> gives IN, the same wikitext as <nowiki>{{UC:{{tc}}}}</nowiki> is expanded to; [[UC:]] is applied to the output "'''in'''" of [[m:Template:Tc|Tc]].
*<tt><nowiki>{{subst:ns:{{subst:#expr:2*3}}}}</nowiki></tt> gives File.
*<tt><nowiki>{{ns:{{subst:#expr:2*3}}}}</nowiki></tt> gives wikitext <nowiki>{{ns:6}}</nowiki> rendered as {{ns:6}}.
*<nowiki>{{subst:t1|{{subst:NAMESPACE}}}}</nowiki> gives the wikitext startHelpend (see {{tl|t1}})
*<nowiki>{{subst:t1|{{subst:#expr:3*4}}}}</nowiki> gives the wikitext start12end
*<nowiki>{{subst:t1|{{subst:uc:AbCdEf}}}}</nowiki> gives the wikitext startABCDEFend
*<nowiki>{{subst:#expr:{{subst:3X|11*}}1}}</nowiki> gives the wikitext 1331
*<nowiki>{{subst:UC:{{subst:3X|abc}}}}</nowiki> gives the wikitext ABCABCABC
*<nowiki>{{subst:LC:{{subst:#expr:1/100000}}}}</nowiki> gives the wikitext 1e-05 (see [[LC:]])
*<nowiki>{{subst:#expr:2*{{subst:CURRENTDAY}}}}</nowiki> gives (at the time of writing) the wikitext 30
*<nowiki>{{subst:UC:{{subst:CURRENTDAYNAME}}}}</nowiki> gives (at the time of writing) the wikitext THURSDAY

However:
#<tt><nowiki>{{subst:UC:{{tc}}}}</nowiki></tt> gives the wikitext <tt><nowiki>{{TC}}</nowiki></tt> rendered as {{TC}}.
#<tt><nowiki>{{subst:ns:{{#expr:2*3}}}}</nowiki></tt> stays <nowiki>{{subst:ns:{{#expr:2*3}}}}</nowiki>, rendered as {{subst:ns:{{#expr:2*3}}}} (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 <tt>x:</tt> in a nested
{{<small>&#160;</small><tt>x:'''...'''</tt>{{<small>&#160;</small><tt>y:'''...'''</tt>}}<small>&#160;</small>}} often is only suitable if all inner <tt>y:</tt> are also substituted.

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

Examples:
*<nowiki>{{#expr:2*{{{p}}}}}</nowiki> gives {{#expr:2*{{{p}}}}}
*<nowiki>{{#expr:2*{{{p|3}}}}}</nowiki> gives {{#expr:2*{{{p|3}}}}}
*<nowiki>{{subst:#expr:2*{{{p|3}}}}}</nowiki> gives <strong class="error">Expression error: Unrecognised punctuation character "{"</strong>
*substituting a template containing <nowiki>{{<includeonly>subst:</includeonly>#expr:2*{{{p|3}}}}}</nowiki> or <nowiki>{{{{{subst}}}#expr:2*{{{p|3}}}}}</nowiki> gives 6 if p is not assigned a value, and twice the number p if it ''is'' assigned a value.

Compare:
*<nowiki>{{uc:2*{{{p}}}}}</nowiki> gives {{uc:2*{{{p}}}}}
*<nowiki>{{uc:2*{{{p|q}}}}}</nowiki> gives {{uc:2*{{{p|q}}}}}
*<nowiki>{{subst:uc:2*{{{p|q}}}}}</nowiki> gives the wikitext <nowiki>2*{{{P|Q}}}</nowiki> rendered as 2*{{{P|Q}}}

and also (from above):
*<tt><nowiki>{{subst:UC:{{subst:tc}}}}</nowiki></tt> gives IN, just like <nowiki>{{UC:{{tc}}}}</nowiki> does; [[Help:Parser function|UC]] is applied to the output "'''in'''" of [[m:Template:Tc|Tc]].
*<tt><nowiki>{{subst:UC:{{tc}}}}</nowiki></tt> gives the wikitext <tt><nowiki>{{TC}}</nowiki></tt> rendered as {{TC}}.

In the substitution of UC, the inclusion tag <nowiki>{{tc}}</nowiki> is treated as string just like <nowiki>{{{p|q}}}</nowiki>.

==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 [[w:en:Function composition|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:
*<nowiki>{{subst:t}}}}</nowiki> gives the wikitext <nowiki>start-{{{1|pqr}}}-end</nowiki>, just that of {{tim|t}}, without noinclude parts and includeonly tags
*<nowiki>{{subst:t|a{{{p|q}}}b}}</nowiki> gives the wikitext <nowiki>start-a{{{p|q}}}b-end</nowiki>

Examples with double substitution:
*<nowiki>{{subst:3X|{{subst:t}}}}</nowiki> gives the wikitext <nowiki>start-{{{1|pqr}}}-endstart-{{{1|pqr}}}-endstart-{{{1|pqr}}}-endstart-{{{1|q}}}-end</nowiki>
*<nowiki>{{subst:3X|{{subst:t|{{{1|q}}}}}}}</nowiki> gives the wikitext <nowiki>start-{{{1|q}}}-endstart-{{{1|q}}}-endstart-{{{1|q}}}-end</nowiki>

==Multilevel substitution==
When substituting a template it may be desirable to carry out a substitution inside the template too. This can be done with "safesubst:" in the template. To prevent premature substitution (i.e., when the template is saved), and to control whether single- or multilevel substitution is applied, this code is provided as default value of an otherwise unused parameter. Since the empty string is a possible but for other purposes uncommon parameter name, it is usually a suitable choice for the name of this parameter, so we can use the code <code><nowiki>{{{|safesubst:}}}</nowiki></code>.

The difference with <code><nowiki>{{{|subst:}}}</nowiki></code> is that <code><nowiki>{{{|safesubst:}}}</nowiki></code>, evaluating to <code>safesubst:</code> if the parameter with the empty string as name is undefined, not only allows multilevel substitution but also multilevel transclusion, because on transclusion it is ignored. With <code><nowiki>{{{|safesubst:}}}</nowiki></code> it is still possible to perform one-level substitution by assigning to the parameter with the empty string as name, the empty string as value, e.g. <code><nowiki>{{subst:if|=|expr|1|a}}</nowiki></code>.

Sometimes a template call defines a value of the parameter with the empty string as name, just for inserting this value as comment inside the template tag, or for lay-out of the template tag, see [[Help:Template#Template tag lay-out|template tag lay-out]]. When enabling multilevel substitution for such a template, one of the two parameters needs to be given another name.

If the name of the inner template, parser function or variable depends on a parameter without default, then we can simply put <code><nowiki>safesubst:</nowiki></code>, because premature substitution is not possible anyway. However, this does not allow us to control whether single- or multilevel substitution is applied,

==Multilevel substitution with independent control of each substitution separately==
Parameters subst1, subst2 (or whatever names one chooses) can be used with "safesubst:" and the empty string as possible values. Thus we can control for each template, parser function and variable separately (or group them, and control per group) whether it is substituted too when the outer template is substituted. Either possibilty can be made the default.

Inner templates with parameters may control 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.

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 <nowiki>{{{subst1|}}}</nowiki>; for calling T we can use:
**<nowiki>{{t|..}}</nowiki> (no substitution)
**<nowiki>{{subst:t|..}}</nowiki> (one-level substitution)
**<nowiki>{{subst:t|subst1=subst:|..}}</nowiki> (two-level substitution)
**<nowiki>{{subst:t|subst1=safesubst:|..}}</nowiki> (ditto)
*with default "safesubst:", T calls inner templates and parser functions prefixing their names with <nowiki>{{{subst1|safesubst:}}}</nowiki>; for calling T we can use
**<nowiki>{{t|..}}</nowiki> (no substitution)
**<nowiki>{{subst:t|subst1=|..}}</nowiki> (one-level substitution)
**<nowiki>{{subst:t|..}}</nowiki> (two-level 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=<nowiki>{{{subst1|}}}</nowiki> or subst2=<nowiki>{{{subst1|safesubst:}}}</nowiki>, respectively ([[Help:parser function|parser function]]s and [[Help:variable|variable]]s don't get the additional parameter).

See also [[Help:Calculation#Substitution]] and {{tim|Example table with computations, with optional substitution}}.

===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 [[m:ParserFunctions#.23switch:|#switch:]] like {{tim|oom}}:
# Development code:<br /><code><nowiki>{{#switch: {{oom|</nowiki></code>''parameter tag'' <code><nowiki>}}|0=</nowiki></code>''case 0 etc.''<code><nowiki>}}</nowiki><code>
# Standard solution:<br /><code><nowiki>{{{{{subst|}}}#switch: {{{{{subst|}}}oom|</nowiki></code>''parameter tag'' <code><nowiki>|subst={{{subst|}}}}}|0=</nowiki></code>''case 0 etc.''<code><nowiki>}}</nowiki><code>
# Better solution: create template code by applying substitution using this wikitext:<br /><code><nowiki>{{{{{subst|}}}#switch: {{subst:oom|</nowiki></code>''parameter tag'' <code><nowiki>}}|0=</nowiki></code>''case 0 etc.''<code><nowiki>}}</nowiki><code>
{{h:mlm|Template:oom}} is prepared for optional substitution, therefore both solutions work, but the latter solution substituting its code is simpler and more efficient.

{{h:|See {{h:mlm|Template:csn}} and {{h:mlm|Template:lz12}} for cases where Template:oom was substituted in this way.}}

If a template uses a parameter whose name is an expression containing a template or parser function, and the template is called with a corresponding parameter definition (in terms of the final name of the parameter) it expands properly only if at the time of expansion of the template the expression for the name of the parameter is or has been evaluated. Thus if the template is substituted without substituting the expression for the parameter name, the parameter definition is "lost", so the parameter becomes undefined. Therefore in such a case no substitution can give the same rendered result as full substitution, while partial substitution gives a different result. See e.g. {{tim|ts1}}.

===Composite operations===
By <nowiki>{{A|{{B|p}}}}</nowiki> a template A is called with, as parameter, a call of template B with a parameter p. We could integrate such template calls to a single call <nowiki>{{C|p}}</nowiki> of a "[[w:en:Function composition|composite template]]" C with parameter p.

;The wikitext for template C would be <nowiki>{{A|{{B|{{{1}}}}}}}</nowiki>, or with optional substitution the following construct&#58;
;<big>{&#123;<b><small>&#160;{&#123;{subst</small><tt>|</tt><small>}&#125;}&#160;</small></b>A|{&#123;<b><small>&#160;{&#123;{subst</small><tt>|</tt><small>}&#125;}&#160;</small></b>B|{{{1}}}<b><small>&#160;|subst={&#123;{subst</small><tt>|</tt><small>}&#125;}&#160;</small></b>&#125;}<b><small>&#160;|subst={&#123;{subst</small><tt>|</tt><small>}&#125;}&#160;</small></b>&#125;}</big>
;The <b>subst=<nowiki>{{{subst|}}}</nowiki></b> is only necessary for recursive substitution as explained above.

Note that it is not useful to specify "<nowiki>{{subst|subst:}}</nowiki>" since in the substitution phase this tag does not reduce to the default "subst:".

If A and/or B is predefined the construct is similar, but without subst=<nowiki>{{{subst|}}}</nowiki> for that template.

==Includeonly==
A more primitive method of multi-level substitution, without the option of single-level substitution, is with a pair of includeonly tags. Substitution is prevented by having the template call inside these tags. Substitution is also prevented by having one or both tags anywhere in the template call except inside a parameter definition. Thus the tag(s) can be before, inside, or after "safesubst:" or "subst:", or inside or after the template name. The positions of the two tags only influence the rendering of the template page itself.

The form {&#123;<code><nowiki><includeonly>safesubst:</includeonly></nowiki></code>something&#125;} suggests that substitution is prevented by discarding "safesubst:" on the page itself, but actually substitution is prevented because the safesubst-syntax is disturbed by the tags.

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.

For examples see "preload" in [[mw:Extension:InputBox#Parameters|Extension:InputBox]] and "substitution" in [[Help:Variable#Substitution|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 {{tiw|en|Conv-dist}}.
This technique is essential for templates like {{tiw|en|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><nowiki>{{{{{subst|}}}ifdef|{{{{{subst|subst:}}}ns:0}}|'''Warning'''}}</nowiki></tt>.
:#Output for {&#123;T&#125;} or {&#123;subst:T&#125;}&#58; '''Warning''',
:#output for {&#123;T|subst=subst:&#125;}&#58; {{{{{subst|}}}ifdef|{{{{{subst|subst:}}}ns:0}}|'''Warning'''}},
:#output for {&#123;subst:T|subst=subst:&#125;}&#58; nothing (no remaining wikitext).
;This is a rare case where replacing [[m:Template:ifdef|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. Pages containing <nowiki>{{A|2=q}}</nowiki> and <nowiki>{{A|1={{{1}}}|2=q}}</nowiki> are rendered the same, but the second has a parameter {{{1}}} while the first does not. See e.g. {{tim|t ps}}.

However, with substitution (using "subst:" or [[Special:Expandtemplates]]) the resulting wikitext is the same, without distinction between a text {{{1}}} and a parameter, it is a parameter anyway, so "1={{{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 p{{{1}}}q{{{2}}}r substituted with 1=u, 2=v gives puqvr; substituted with 2=v it gives p{{{1}}}qvr, which itself, substituted with 1=u gives also puqvr.
*Two-level substitution of a template containing <nowiki>{{#if:{{{4}}}|{{{3}}}p}}</nowiki> with 3=u, 4=v gives up; substituted with 4=v it gives {{{3}}}p, which itself, substituted with 3=u gives up.

Examples without equality:
*Two-level substitution of a template containing <nowiki>{{#if:{{{3}}}|{{{4}}}p}}</nowiki> 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 <nowiki>{{#if:{{{2}}}|{{{1}}}p}}</nowiki> with 1=u, 2=v gives up; substituted with 2=v it gives {{{1}}}pp (the bug), which itself, substituted with 3=u, gives upp.
*Two-level substitution of a template containing <nowiki>{{#expr:{{{1}}}*{{{2}}}}}</nowiki> 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. {{{1}}} by <nowiki>{{{1{{{{{substvoid|}}}void}}}}}</nowiki> 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 {{{1}}}.

With defaults:

Rendered the same as without substitution:
*Two-level substitution of a template containing <nowiki>p{{{1|d}}}q{{{2}}}r with 2=v gives p{{{1|d}}}qvr</nowiki>.
*Two-level substitution of a template containing <nowiki>{{#if:{{{4}}}|{{{3|d}}}p}} with 4=v gives {{{3|d}}}p</nowiki>.

Not rendered the same as without substitution:
*Two-level substitution of a template containing <nowiki>{{#if:{{{3|}}}|{{{4}}}p}} with 4=v gives vp</nowiki>.
*Two-level substitution of a template containing <nowiki>{{#if:{{{2}}}|{{{1|d}}}p}} with 2=v gives {{{1|d}}}pp (the bug)</nowiki>.
*Two-level substitution of a template containing <nowiki>{{#expr:{{{1|6}}}*{{{2}}}}}</nowiki> with 2=8 gives &lt;strong class="error">Expression error: Unrecognised punctuation character "{"&lt;/strong>.

After substitution with the parameter definition:
*<nowiki>{{subst:#if:{{{3|}}}|vp}} gives vp</nowiki>.
*<nowiki>{{subst:#if:v|{{{1|d}}}p}} gives {{{1|d}}}pp (the bug)</nowiki>.
*<nowiki>{{subst:#expr:{{{1|6}}}*8}}</nowiki> gives &lt;strong class="error">Expression error: Unrecognised punctuation character "{"&lt;/strong>.

Rewritten:
*<nowiki>{{subst:#if:{{subst:#ifeq:{{{3|+}}}|{{{3|-}}}|vp}}}} gives the empty string</nowiki>.
*<nowiki>{{subst:#if:v|{{subst:#ifeq:{{{1|+}}}|{{{1|-}}}|{{{1}}}|d}}p}} gives dp</nowiki>.
*<nowiki>{{subst:#expr:{{subst:#ifeq:{{{1|+}}}|{{{1|-}}}|{{{1}}}|6}}*8}}</nowiki> gives 48.

==Documenting substitution; preservation of comments==
Usage of a template through <code>subst:</code> does not automatically show up in page histories. Therefore providing the line of wikitext containing "subst:" in the [[Help:edit summary|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. Substitution of parameters inside comment tags does not work.

See also [[Help:Comment tags#Substitution]].

==See also==
*[[mw:Extension:ExpandTemplates]]
*[[w:Help:Substitution#safesubst:]]
*[[w:Wikipedia:Template substitution]] - partly technical, partly policy
*[[bugzilla:2003]] - feature request to allow marking a template as being substituted without "subst:"
*Templates containing a call to itself with "subst:" and producing a similar call with updated info, either replacing or adding to the previous info:
**{{tim|last edit}} - example: {{subst:Last edit|</span>Last edit of page [[Help:Substitution]]: 2010-09-4 T 20:28 [[w:UTC|UTC]], by [[User:Patrick|Patrick]] ([[User talk:Patrick|talk]])<span style="display:none">}}</span>
**{{tim|page history}}
{{h:f|enname=Substitution}}

Latest revision as of 02:09, 25 November 2023