Help:Substitution

From CivWiki
Revision as of 14:27, 12 May 2006 by wikipedia>Omniplex (TRANSWIKI from Meta)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Template:H:h

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: m:Template:t2 (backlinks edit), containing:


start-{{{1}}}-middle-{{{2}}}-end

and called as {{subst:t2|[[a]]|{{x3|b}}}} gives the wikitext start-[[a]]-middle-{{x3|b}}-end, rendering as start-a-middle-Template:X3-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 Template:Timc

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 bugzilla: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:

{{subst:CURRENTDAY}} {{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}},    {{subst:CURRENTTIME}} (UTC)

giving:

10 March 2005,    08:23 (UTC)

{{subst:UC:{{subst:tc}}}} using m:Template:tc (backlinks edit) gives IN.

{{subst:ns:{{subst:#expr:2*3}}}} gives Image.

{{subst:#expr:{{subst:#expr:2*3}}*4}} gives 24.

However:

{{subst:UC:{{tc}}}} gives the wikitext {{TC}}, rendered as Template:TC (UC is applied to the text "{{tc}}", instead of to the resulting string. Thus the result is different from {{UC:{{tc}}}}, giving IN.

{{subst:ns:{{#expr:2*3}}}} stays {{subst:ns:{{#expr:2*3}}}}, rendered as {{subst:ns:6}}.

{{subst:#expr:{{#expr:2*3}}*4}} 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 {{{subst}}} that takes the value "subst:" or "": {{ {{{subst}}}C|..}}. Then B can be included in A with {{B|subst=|..}} or substituted in A with {{subst:B|subst=subst:|..}}. In the case of more levels, C can call D carrying on the value of parameter subst, with {{ {{{subst}}}D|subst={{{subst}}}|..}}, etc. The parameter {{{subst}}} used in B can be given one of the possible values as default: {{{subst|}}} or {{{subst|subst:}}}.

See also m:Template:Substitution demo (backlinks edit) and m:Template:Example table with computations, with optional substitution (backlinks edit).

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 ~<includeonly>~</includeonly>~~ 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:

* [[Wikipedia:Mediation Cabal/Cases/{{<includeonly>subst:</includeonly>CURRENTDAY}} 
{{<includeonly>subst:</includeonly>CURRENTMONTH}} {{<includeonly>subst:</includeonly>CURRENTYEAR}} {{{1}}}|
{{<includeonly>subst:</includeonly>CURRENTDAY}} {{<includeonly>subst:</includeonly>CURRENTMONTH}}
{{<includeonly>subst:</includeonly>CURRENTYEAR}} {{{1}}}]]

is expanded when called as {{subst:medcab1|text}}, e.g. to the wikitext * [[Wikipedia:Mediation Cabal/Cases/19 12 2005 text|19 12 2005 text]] rendered as

See also en:Template:Conv-dist (backlinks edit).

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:

{{subst:t nws}}, using m:Template:t nws (backlinks edit) which refers to m:Template:tc (backlinks edit), gives <nowiki>in</nowiki>, 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, {{subst:tc}} is expanded to "in" anyway.

Template:Timc, and called as Template:Tls2, gives the wikitext <pre>start-{{{1}}}-middle-{{{2}}}-end</pre>

See also

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.

Template:H:f