Help:Template

From Meta, a Wikimedia project coordination wiki
This is an archived version of this page, as edited by Patrick (talk | contribs) at 10:07, 16 December 2007. It may differ significantly from the current version.
Jump to navigation Jump to search
Shortcut:
WM:TEMP

The (whole) contents of one page can be placed onto another page via a process called transclusion. If a page is specially intended for this purpose it is called a template.

This can be considered a wiki subroutine facility and is comparable to an #include statement or macro that is expanded at page view time. Substitution allows templates to be used as a macro facility.

This page and its extension Help:Advanced templates provide a reference manual on templates in MediaWiki. For an introduction, see A quick guide to templates and mw:Help:Templates. For syntax often used within templates, see Help:ParserFunctions and other "Advanced functioning" help pages listed below.

Note: The master version of this page is the page m:Help:Template on Meta-Wiki; template demos work there, but may not work if you are viewing a copy of this page on another project.

General

The template namespace is the namespace with prefix "Template:" (this prefix can be used in all languages, but the canonical prefixes in languages other than English include Vorlage, Modèle, Sjabloon, with colon; the site WikiFAQ calls it Snippet).

A page in this namespace is called a template. The contents of such a page (either fixed or depending on parameters and/or variables) is designed to be useful for insertion in other pages (transclusion, creating a compound document). In a what links here list, the term in MediaWiki:Istemplate is used, by default "inclusion", sometimes "transclusion", currently on this wiki "transclusion".

The syntax for insertion of the page "Template:name" is {{name}}. This is called a template tag, and comprises two instances of the "brace" at either side of the template page name, excluding the "Template:" prefix.

Example: abc{{tc}}def

(using Template:tc containing "<onlyinclude><!-- --><templatestyles src="Template:Nowrap/styles.css"/><!-- --><code class="nowrap"><!-- -->&#123;&#123;<includeonly>{{{1}}}</includeonly><noinclude>tlc&#124;...</noinclude><!-- -->{{#ifeq:{{{2|x}}}|{{{2|}}}| &#124;{{{2}}} }}<!-- -->{{#ifeq:{{{3|x}}}|{{{3|}}}| &#124;{{{3}}} }}<!-- -->{{#ifeq:{{{4|x}}}|{{{4|}}}| &#124;{{{4}}} }}<!-- -->{{#ifeq:{{{5|x}}}|{{{5|}}}| &#124;{{{5}}} }}<!-- -->{{#ifeq:{{{6|x}}}|{{{6|}}}| &#124;{{{6}}} }}<!-- -->{{#ifeq:{{{7|x}}}|{{{7|}}}| &#124;{{{7}}} }}<!-- -->{{#ifeq:{{{8|x}}}|{{{8|}}}| &#124;{{{8}}} }}<!-- -->{{#ifeq:{{{9|x}}}|{{{9|}}}| &#124;{{{9}}} }}<!-- -->&#125;&#125;<!-- --></code><!-- --></onlyinclude> {{Documentation}}")

Gives: abc{{{{{1}}}}}def.

This reference in the wikitext causes insertion of the template content when the referring page is rendered. Various equivalent terms are:

  • inclusion / including the page - "inclusion" by itself may be ambiguous, since one can say that a section etc. is included in a page even if there is no transclusion; based on these terms are the tag names "noinclude" and "includeonly", see below
  • transclusion / transcluding the page - this is more specific than "inclusion"
  • page embedding / embedding the page

Terms too general to be used with "page", therefore only used with "template":

  • template call / calling the template - referring more to the template tag than to the result
  • template use / using the template

Depending on context "including" can be used as opposed to substituting the template.

To create a hyperlink to a template page, use the usual wikilink syntax [[Template:name]].

The pages in the namespaces other from "Template" namespace can also be used as templates, except pages in namespaces specified in $wgNonincludableNamespaces. To use a page in the main namespace as a template, add a colon before the page name.

Also it provides an alternative way of calling a template, which can be used in the case of a name conflict with a variable, e.g. Template:Ns:3 and Template:PAGENAME; while {{Ns:3}} and {{PAGENAME}} give User talk and Template, {{:Template:Ns:3}} and {{:Template:PAGENAME}} give demo and Template.

Using a page outside the template namespace as template can be useful for:

  • quoting interface messages in the MediaWiki namespace
  • templates for personal use in subpages of one's userpage, e.g. for experimenting
  • inclusion of pages in other pages in the same namespace, e.g. having an extra page associated with each page, such as a to-do list, with a simple name correspondence; it can be created and included with {{{{NAMESPACE}}:{{PAGENAME}}/todo}}.

As usual the template name is case sensitive except (on most projects) for the first letter, and a blank space is equivalent with an underscore (as opposed to parameter names, see below: they are case-sensitive, even with respect to the first letter, and spaces are distinguished from underscores).

If the page to be transcluded does not exist, then a link to the edit page is produced (for conversion details see Template:Exists). Thus, one way of making a template is putting the tag first, and then following the link. An exception is that transclusion of a "non-existing" page in the MediaWiki namespace trancludes the default content of that page. In other words, apart from transcluding a page one can also transclude a system message in a specified language (by default the site language); the result is independent of the user-specified interface language.

One can call a template that calls another template. If a template calls itself directly or indirectly, inclusion works only on one level for each template. However, with templates redirecting to the template that one would want to call recursively, one can achieve recursion without having to make copies of the whole template content, with the number of levels limited by the number of redirects. See also Repetition within a page and Template:List of template calls [ talk edit history links ].

A variable in a template is evaluated after insertion in the referring page, i.e. if {{PAGENAME}} occurs in the wikitext of a template, it is rendered as the name of the referring page, not the name of the template.

A list of pages embedded in a page (as recorded in the templatelinks table), all with links, is given on the edit page. Notes:

  • on a section edit page first the whole list is given, i.e. also the templates used in other sections; after pressing preview only those in the section, updated for the edits made, if any; preview on first edit gives the full list;
  • similarly, when editing an old version of a page, the list is first given for the current version, while after pressing preview the templates that used to be included are shown, updated for the edits on the old version made in the current session, if any;
  • if a page is conditionally embedded through #if, #ifeq, #ifexist, #ifexpr, or #switch, it is counted as embedded, even if the condition is not fulfilled
  • if the name of an embedded page is an expression, e.g. {{abc{{CURRENTDAY}}}}, currently giving Template:Abc23, or {{{{#ifexist:Template:A|A|x1}}|Template does not exist}}, currently giving
Template does not exist, the current template name is shown.

Thus, to use "What links here" to find the pages that actually embed template A, instead of using e.g.

{{#if:..|{{a|pa1}}|{{b|pb1}}}}

one has to use an alternative, e.g., duplicating the condition:

{{{{#if:..|a|b}}|{{#if:..|pa1|pb1}}}}

or, using Template:echo [ talk edit history links ]:

{{{{#if:..|a|echo}}|1=pa1|echo={{b|pb1}}}}

Only with the first of the two alternatives "What links here" also finds the pages that actually embed template B. In the second alternative the page is always considered embedded. If we replace "b" by "{{if:1|b}}" the page is never considered embedded.

(Supported in version 1.4 and higher.)

When saving a page without newline at the end, and going to the edit page again, there is a newline at the end. However, this does not seem to have effect, see the example above and the following example:

{{Numbered list demo}}{{Numbered list demo}}
{{Numbered list demo}}

that uses Template:Numbered list demo three times, gives

  1. India
  2. Beverungen
  3. Prague
  4. India
  5. Beverungen
  6. Prague
  7. India
  8. Beverungen
  9. Prague

In a template name the character "#" and any characters after that are ignored: "{{tc#abc}}" gives "{{{{{1}}}}}". This is used in en:Template:hif [ talk edit history links ].

A wikitext with consecutive double opening braces and consecutive double closing braces is interpreted as containing a template parameter tag with triple braces. To avoid that, use a space in at least one of the two quadruples. For example, "{{{{tc}}}}" gives "{{{{tc}}}}", while "{{ {{tc}}}}" gives "{{ {{{{{1}}}}}}}".

With Special:ExpandTemplates one can view the expanded wikitext a template call produces, as intermediate step toward rendering. The same is also produced with a URL like //meta.wikimedia.org/w/index.php?title=Help:Template&action=raw&templates=expand.

Use of templates across projects

A template only works when referred to from a page in the same project. To use it in another project, one has to copy it to there. Bugzilla:1126 is a request to lift that restriction, i.e. allow interwiki use of templates.

The adjustments needed in the copy, to work on the other project, can be reduced by using {{SERVER}}, localurl, and generic namespace names, and writing links in a way that works on multiple projects, e.g. m:Help:Table. For copying multiple templates, export and import can be helpful. It is also convenient if the template names can be the same on the other project. Therefore, when choosing template names, check which names are in use on other projects to which people might want to copy the templates. Note that if a template is referred to by something like abc{{NAMESPACE}}, a project with different namespace names requires a different template name, or a redirect.

Transclusion across projects, hence changing contents from a central place, is only possible:

  • for content in the form of an image on Commons, to any page,
  • for editable content on an image page on Commons, including content transcluded from other pages on Commons, to the corresponding local image pages, if there is no local image with the same name

Templates on the central Wikia work in the separate Wikia projects, provided that the prefix "wikia:" is used, see shared templates on wikia. (Please note that these external Wikia projects are NOT hosted and managed by the Wikimedia Foundation.)

Sometimes multiple projects require pages which are partly the same and partly different. For example, on some projects "m:" is used as interwiki link to Meta, and on some projects "MetaWikipedia:". In this case a common wikitext can be used, calling project-specific templates. In the example the wikitext of the page could contain {{meta}}, and we would have on each wiki a Template:Meta containing the required prefix.

Parameters

Template parameters are called-by-value, and therefore input parameters only (see also the section "Template expansion" below). They are either named or implicitly numbered 1, 2, 3, ...

Comparison between named or implicitly numbered parameters:

  • With implicit numbering the call is shorter
  • Named parameters can be specified in any order.
  • If a parameter value contains "=" the name has to be put explicitly, even if it is a number (see also below).
  • Spaces and newlines are stripped from the start and end of parameter values of named parameters, but not of implicitly numbered parameters

In the template definition, the formal parameter (the placeholder for the parameter value) is a parameter name with three pairs of braces. So, for example, you would write {{{myVariableName}}} for a template parameter which you wanted to call myVariableName.


MediaWiki version: 1.6 (for parameters defaults)

In the template call, the syntax is either

  • {{templatename|parname1=parvalue1|parname2=parvalue2}} with tags {{{parname1|default}}}, {{{parname2|default}}}, etc. in the template, or
  • {{templatename|parvalue1|parvalue2}} with tags {{{1|default}}}, {{{2|default}}}, etc. in the template.

The default (optional, together with the pipe character in front) can be different for every occurrence, and applies if no value is specified when calling the template, not to be confused with the case that the empty value is specified.

To distinguish between a parameter being defined and non-empty on one hand, or undefined or empty on the other hand, use #if: with a blank default:

{{ #if: {{{param|}}} | param is defined and non-empty | param is undefined or empty}}.

To distinguish between defined (and possibly empty) and undefined, use:

{{ #ifeq: {{{param|+}}} | {{{param|-}}} | param is defined | param is undefined }}.

In the case of page substitution with an undefined parameter, not the default but the parameter itself with default is substituted into the wikitext. If this is not desired, see Help:Substitution#Parameter_default_considerations for an alternative (it also uses the #ifeq just mentioned).

Monitoring parameter usage

To monitor the usage of a parameter of a template (in the case that many pages use the template), this template can call an auxiliary template of which the name depends on whether the parameter is defined. The possible auxiliary templates need not exist, if they are e.g. used as parameter value of Template:Void, to avoid displaying anything. Applying "What links here" to the possible auxiliary templates shows which pages use the template with the parameter, and which pages use the template without the parameter. This parameter usage monitor facility should preferably be built into the template when creating the template or introducing a new parameter. If it is done afterwards "What links here" may not show a page until it has been refreshed by an ordinary or dummy edit or purge.

Applications of monitoring parameter usage include:

  • check where the parameter has not yet been specified, in order to add it
  • check where a parameter is used that is renamed, in order to change the template calls
  • check whether a parameter is used before abolishing it
  • when a template has been changed with respect to processing a particular parameter, check the pages which use the parameter (or some of them) to see if things work as desired.

In a similar way an auxiliary template can be called for a "template / parameter / parameter value" combination or "template / parameter / parameter value range" combination. "What links here" then shows which pages use the template with this parameter value, or with the parameter in the given range.

See Template:T optional parameter demo.

Example: named parameter substitution

Let's create a template called Name-example (that is, the template will be Template:Name-example), with a parameter for the first name which we will call firstName, and a parameter for the last name which we will call lastName.

Type the following in the new Template:Name-example:

:I am a template example, my first name is '''{{{firstName}}}''' and my last name is '''{{{lastName}}}'''.
:
:You can reference my page at [[{{{lastName}}}, {{{firstName}}}]].

On a second page, type this: {{Name-example}}

The result is:

I am a template example, my first name is {{{firstName}}} and my last name is {{{lastName}}}.
You can reference my page at [[{{{lastName}}}, {{{firstName}}}]].

Because the template has no parameters.

But if we type this on the second page: {{Name-example | firstName=John | lastName=Smith}}

The result will looks like this:

I am a template example, my first name is John and my last name is Smith.
You can reference my page at Smith, John.

To analyse the working, one can apply Special:ExpandTemplates to see the expanded wikitext as intermediate result:

:I am a template example, my first name is '''John''' and my last name is '''Smith'''.
:
:You can reference my page at [[Smith, John]].

When this wikitext is applied directly the resulting rendering is the same.

Example: numeric parameter substitution

An example of how the contents of a parameter function.

The simplest case

Let us first define a template with the name t0, which will

  • expect a single argument
  • give back the text of that argument, with no spaces before or after it, between "start-" and "-end":
start-{{{1}}}-end

the definition of course being on page Template:t0.

  1. Calling the template named t0 with a parameter value of "a", i.e.,
    {{t0|a}}
    gives
    "start-a-end"
  2. Calling the template named t0 with a parameter value of " " (a blank space), i.e.,
    {{t0| }}
    gives
    "start- -end" .
  3. Calling the template named t0 with a parameter value of "" (an empty string), i.e.,
    {{t0|}}
    gives
    "start--end".
  4. Calling the template named t0 and not passing any parameters, i.e.,
    {{t0}}
    gives
    "start-{{{1}}}-end".

If something like {{{1}}} or in fact any {{{name}}} is visible on an ordinary page it typically indicates that a mandatory parameter of a template used on this page isn't defined.

Stripping of spaces and newlines

Calling the template named t0 with a parameter which consists of some spaces or two newlines:

{{t0|     }}

gives

start-     -end
{{t0|

}}

gives

start-

-end
{{t0|1=     }}

gives

start--end
{{t0|1=

}}

gives

start--end

Thus, spaces and newlines are stripped from the start and end of parameter values of named parameters, but not of unnamed parameters! This is probably a bug. See Help:Newlines and spaces. Also, compare conditional whitespace.

Declaring a default value

Main article: Help:Parameter default.

Now let us define a similar template with the name t which contains a single numbered parameter 1 with a default of pqr. The only difference between the effects of t and t0 appears when they are called with no parameter (and no "|"):

start-{{{1|pqr}}}-end

the definition of course being on page Template:t.

  1. Calling the template named t with a parameter value of "a", i.e.,
    {{t|a}}
    gives
    "start-a-end".
  2. Calling the template named t with a parameter value of " ", i.e.,
    {{t| }}
    gives
    "start- -end".
  3. Calling the template named t with a parameter value of "", i.e.,
    {{t|}}
    gives
    "start--end".
  4. Calling the template named t with named parameter 1=, i.e.,
    {{t|1=no surprise}}
    gives
    "start-no surprise-end".
  5. Calling the template named t with 1= after an unnamed parameter, i.e.,
    {{t|no|1=surprise}}
    gives
    "start-surprise-end".
  6. Calling the template named t with 1= before an unnamed parameter, i.e.,
    {{t|1=no|surprise}}
    gives
    "start-surprise-end".
  7. Calling the template named t and no parameter at all, i.e.,
    {{t}}
    gives
    "start-pqr-end".
  8. Calling the template named t and no named or unnamed parameter 1, i.e.,
    {{t|2=two}}
    gives
    "start-pqr-end".

Restrictions on parameter values

  • If a parameter value contains an equals sign, the parameter name must be put in explicitly, even when it is 1, 2, 3, etc. (Alternately, the equals sign could be replaced by the HTML entity &#x3d;)
  • Any unmatched pairs of two consecutive braces or brackets must be placed in nowiki tags. Braces can be used for nested template parameters, nested templates or parser functions while brackets can be used for links. Unmatched pairs not placed in nowiki tags either prevent template expansion or are taken as closing braces for the template call.
    • For example, using Template:tlis containing "[[a{{{1}}}b]]":
    • {{tlis|abc]]def[[ghi}} gives [[a{{{1}}}b]].

Internal links

In the case of an internal link in a template with the target depending on a parameter, and in the case of a link with the target depending on a template, existence detection works as usual.

Examples:

  • {{la|Help:Tabl|tabl}} (using Template:la containing "<noinclude>For language icon of Latin language, see {{tl|Latina}}.<br></noinclude>

<span class="plainlinks"><!-- -->{{#ifexist: {{{1|Article}}} <s>| |}}<!-- -->[[{{ucfirst:{{{1|Article}}}}}]]<!-- -->{{#ifexist: {{{1|Article}}} </s> | |}}<!-- --> ([{{fullurl:{{ucfirst:{{{1|Article}}}}}|action=edit}} edit] · [[{{TALKPAGENAME:{{ucfirst:{{{1|Article}}}}}}}|talk]] · [{{fullurl:{{ucfirst:{{{1|Article}}}}}|action=history}} history] · [{{fullurl:Special:Whatlinkshere/{{ucfirst:{{{1|Article}}}}}|limit=999}} links] · [{{fullurl:{{ucfirst:{{{1|Article}}}}}|action=watch}} watch] · [{{fullurl:Special:Log|page={{urlencode:{{ucfirst:{{{1|Article}}}}}}}}} logs] · [{{fullurl:{{ucfirst:{{{1|Article}}}}}|action=delete}} delete]) {{#ifexist: {{{1|Article}}} '''Deleted!'''| |}}</span><br>") gives: Help:Tabl (edit · talk · history · links · watch · logs · delete)

Templates and external links

Examples:

However, a URL can not be composed of:

  • a first part in a parameter and a second part added by the template (although we have seen above that reversed it works: a second part in a parameter and a first part prefixed by the template)
  • a first part in a template and a second part after the template (although we have seen above that reversed it works: the second part in a template and the first part before the template)

Example of first restriction:

Examples of second restriction:

  • [{{th}}add_at_the_end] (using Template:th containing "{{Languageicon|th|{{#language:th|{{Pagelang}}}}}}<noinclude>[[Category:Language icons|th]]</noinclude>")} gives [(Thai)add_at_the_end]
  • "{{th}}add_at_the_end" gives "(Thai)add_at_the_end"

Noinclude, includeonly, and onlyinclude

This feature is not available before version 1.6. These features are applied as nesting pairs, or 'Blocks', as it is said they enclose the material so that they can be said to begin and end a block of wikitext (code).

noinclude blocks

Anything between <noinclude> and </noinclude> will be processed and displayed only when the page is being viewed directly; it will not be included or substituted. Possible applications are:

  1. Categorising templates, see template documentation.
  2. Interlanguage links to similar templates in other languages.
  3. Pages in the MediaWiki namespace.
includeonly blocks

The converse is <includeonly>. Text between <includeonly> and </includeonly> will be processed and displayed only when the page is being included. Applications include:

  1. Adding all pages containing a given template to a category, but not the template itself.
  2. Avoiding messy rendering on the template page, e.g. {{#expr:{{{1}}}}} gives Expression error: Unrecognized punctuation character "{". [1].

Note that spaces and newlines between the general content and the tagged part belong to the general content. If they are not desired the include tag should directly follow the content on the same line:

<noinclude>this is </noinclude>fine<includeonly>, closing tags are
</includeonly><noinclude>
less critical, but must be specified.</noinclude>
nesting

Nesting an includeonly block within a noinclude block (or vice-versa) is legal but pointless.

onlyinclude blocks

With <onlyinclude>wikitext</onlyinclude> on a page, the display of the wikitext so surrounded and the rest of the page (except includeonly parts) is rendered on the page itself normally (note: this means interwiki translation links will behave as normal external links, unless they are further bracketed by <includeonly>). The demo page below holds a full screenful of text and code, including several includeonly blocks as well as a single onlyinclude block as a demonstration. It is advisable to look at that page in edit mode.

However, in transclusion, the page gives only the parts within onlyinclude blocks. Using the test line:

Using [[Help:Template/onlyinclude demo]], 
     <nowiki>{{Help:Template/onlyinclude demo}}</nowiki> 
          gives '''{{Help:Template/onlyinclude demo}}'''. 

When repeated below we get: Using Help:Template/onlyinclude demo, {{Help:Template/onlyinclude demo}} gives def. When that page is included, it does not even transclude other blocks which are defined as includeonly parts, unless such are also within the onlyinclude block. So on a page with onlyinclude tags, text within includeonly tags is never rendered, i.e., it is reduced to wikitext comment. See also Help:Template/onlyinclude demo 2 and its talk page.

Caveats on splitting and interworkings

Attempts to nest split pairs of these tags, or the similar <nowiki> and </nowiki> pair, won't work as expected.

If say <nowiki> begins within the general page content, or in a "noinclude" part, or in an "includeonly" part, then it also has to be closed within the same part.

The code ~<includeonly>~</includeonly>~~ will be displayed as ~~~ when the template is not included, ~~~~ when the template is included, but it will only be expanded as the active user when the template is subst'd, which is to say when it has been joined within the same block once again.

Wiki markup at the beginning of a template

If the first included character of a template is one of the Wiki markup characters :;*#, then it's interpreted as being at the beginning of the line (even when the template call is not).

To avoid this effect use <nowiki>#</nowiki> or a numeric character reference (NCR) or HTML entity like say &#58; for a colon. This NCR is also useful in conjunction with definition lists.

Substitution

Main article: Help:Substitution

Putting "subst:" after the double opening braces causes an automatic conversion of wikitext when the referring page is saved: the subst tag is replaced by the wikitext of the template, with the parameter values substituted for the parameters.

For optional multi-level substitution of templates add {{subst|}} after the opening braces in all calls of templates, parser functions and variables in templates, and add "subst=subst:" to the parameter definitions of all templates. Use additional similar parameters for selective substitution.

Also use the alternative way of specifying a parameter default: always except for {{{subst|}}}, or just in cases where it is needed as workaround for the bug regarding an undefined parameter with a default in a parameter of a parser functions, i.e., in parser functions, if the parameter name is one of the affected ones.

Redirection

When a page called for inclusion is a redirect page, the redirect target is included instead. As usual, a double redirect does not work.

A page that consists of nothing else than the inclusion of another page as a template, shows similarities to a redirect to that other page, but also many differences, including:

  • the result has as header the name of the referring page
  • there is no redirect message
  • the buttons such as edit (for editing the whole page), watch, talk, history, "what links here", and "last modified", etc., refer to the referring page (to go to the target page, a section edit link can be used, from there the whole page can be accessed)
  • the referring page is in the same categories as the target page, except when includeonly and/or noinclude tags are used
  • "double redirects", with one or both being this kind of "pseudo-redirect", work.
  • for the Template:Peisl it is disadvantageous to include the target page: the pre-expand include size is the sum of the pre-expand include size of the target page and the size of its wikitext; since typically the wikitext of a page is much smaller than this maximum, this is only an extra complication if the pre-expand include size of the target page is already close to the maximum.


Compare Help:Template namespace with Help:Template namespace - demo of alternative for redirect.

In a page that in turn includes such a referring page there is no difference.


Note that embedding works for all pages where redirecting works, and it also fails for all pages where a redirect won't work.

Self-transclusion

A page can transclude itself, but is protected against an infinite loop: the transclusion in the transcluded copy of the template is replaced by the bolded pagename. See Help:Self-transclusion demo (talk, backlinks, edit) and Help:Self-transclusion demo 2 (talk, backlinks, edit).

Transcluding on page A page B redirecting to page A allows more repetition, see Help:Self-transclusion demo with redirect (talk, backlinks, edit) with Help:Self-transclusion demo redirect (talk, backlinks, edit).

If due to the use of braces hidden in a template (see Help:Recursive conversion of wikitext) the saved wikitext of a page contains a substitution call to itself, then on the next save the substitution is applied for one level; the transclusion in the transcluded copy of the template is replaced by a link to the pagename. Thus it becomes a self link, i.e., it becomes bolded text with immediately following characters also bolded. This time the link is followed in the wikitext by the hidden message "<!-- WARNING: template loop detected -->"; see [2]; this edit was automatic by edit/save without making a change in the wikitext oneself.

Parsing braces

If some braces are "hidden" in a template, as in Template:lb [ talk edit history links ], then parsing of pairs of double and triple braces is based on visible braces only. In the case of partial substitution, first parsing is done for the purpose of substitution, then parsing is done again for rendering:

  • {{t1|{{lb}}tc}}}} gives start-{{tc-end}} - the last-but-one pair of closing braces is taken as closing braces of Template:t1 [ talk edit history links ]
  • {{t1|{{subst:lb}}tc}}}} gives the wikitext {{t1|{{tc}}}} rendered as start-{{{{{1}}}}}-end - after substitution the last pair of closing braces is taken as closing braces of t1
  • {{subst:t1|{{subst:lb}}tc}}}} gives the wikitext start{{tcend}} rendered as startTemplate:Tcend - the last-but-one pair of closing braces is taken as closing braces of t1 for both substitutions
  • {{subst:subst}}t1|a}}, substituting Template:subst [ talk edit history links ], gives the wikitext {{subst:t1|a}} rendered the same; in the next edit the wikitext is converted to "startaend".
  • {{subst:tsubst 1|a}}, substituting Template:tsubst 1 [ talk edit history links ]: ditto.

With parser function #if instead of template:t1:

  • {{#if:x|{{lb}}t1|a}}|b}} gives {{t1|b}} - the last-but-one pair of closing braces is taken as closing braces of #if.
  • {{#if:x|{{subst:lb}}t1|a}}|b}} gives the wikitext {{#if:x|{{t1|a}}|b}} rendered as start-a-end - after substitution the last pair of closing braces is taken as closing braces of #if.
  • {{subst:#if:x|{{subst:lb}}t1|a}}|b}} gives the wikitext {{t1|b}} rendered as start-b-end - the last-but-one pair of closing braces is taken as closing braces of #if for both substitutions.
  • {{subst:#if:x|{{subst:subst}}t1|a}}|b}} gives the wikitext "{{subst:t1|b}}" rendered the same: "{{subst:t1|b}}" (the substitution phase is finished; the ordinary template expansion phase ignores substitution calls); in the next edit the wikitext is converted to "startbend".
  • {{subst:#if:x|{{subst:#if:x|{{subst:subst}}t1|a}}|b}}|c}} gives the wikitext "{{subst:t1|c}}" rendered the same: "{{subst:t1|c}}".
  • {{subst:#if:x|{{subst:tsubst 1|a}}|b}} gives the wikitext "startaend". Apparently there is a substitution phase of parameter expansion resulting in the parameter value "{{subst:t1|a}}", and another substitution phase for the substitution of #if, taking the parameter value not as plain text, but interpreting the "subst:" in it again.

See also Help:Recursive conversion of wikitext.

Comparison of linking and embedding

Linking and embedding have in common:

  • if the page does not exist, a link to the edit page is shown; the links are identical if it is not a piped link; the link does not show parameters and does not show whether, after creation, the new page will be linked or embedded.
  • the name can depend on variables and parameters

Predefined templates

Variables and parser functions can be considered predefined templates. They do not have a page that defines in terms of wikitext what they do, because their action is something extra that cannot be constructed from more basic wikitext functionality.

Some additional differences:

  • Parser functions use ":" instead of the first "|".
  • An edit page does not list predefined templates used on the page.
  • No "What links here" is available for predefined templates, to find the pages where they are used.
  • Apart from #switch, predefined templates don't allow you to define named parameters, and therefore equal signs generally have no special effect, comparison:
    {{ #if: not blank | 2=abc }} gives 2=abc
    {{ ifdef | not blank | 2=abc }} gives abc
  • It's however possible to corrupt the value shown for undefined parameters without default, for more examples see Template:ifeq demo [ talk edit history links ]:
    {{ #if: {{ns:0}} | 2=xy | {{{2|blank}}} }} gives blank
    {{ #if: {{ns:0}} | 2=xy | {{{2 }}} }} gives {{{2 }}}
    {{ #if: {{ns:0}} | oops | {{{1|blank}}} }} gives blank
    {{ #if: {{ns:0}} | oops | {{{1 }}} }} gives {{{1 }}}

Ordinary and predefined templates have in common that their parameter values can depend on ordinary and predefined templates. In the case of named parameters, the names used in the template call can also depend on ordinary and predefined templates. This applies also for the named parameters of #switch.

Template expansion

Expanding a template call internally involves, to start with, identifying the parameter name part and each parameter definition part, based on separators "|", excluding those inside pairs of double and triple braces and double square brackets (as far as explicit in the wikitext of the outer template call, hence not counting "|" in e.g. the content of an inner template). Each part is expanded, after which each parameter definition part is separated, if applicable, based on the leftmost "=", into a parameter name and a parameter value. Executing these parameter definitions from left to right, an associative array of (name, value) pairs is constructed with unique names, including numbers for unnamed parameters.

Expanding a predefined template and expanding a template parameter tag involves the same, as far as applicable.

It follows from the above that a "|" in the expression for the value of a parameter or parameter default can only be inside pairs of double and triple braces and double square brackets. To obtain a parameter definition which expands to wikitext containing "|", this character has to be in the content of an inner template, parser function, or parameter value, see e.g. Template:! [ talk edit history links ]..

Expansion of wikitext is relevant even if the wikitext resulting from these subevaluations cannot have effect on the overall wikitext, as in the case of #ifexpr etc. for either the then-part or the else-part, and in the case of an unused template parameter:

  • Subevaluations involving template calls add to the pre-expand include size (see w:Wikipedia:Template limits).
  • Side effects:
    • Subevaluations producing links add on the target page to the list of incoming links (What links here), based on the pagelinks table, even if there is no link on the resulting page.
    • Subevaluations involving template calls add on the template page in "What links here" and on the edit page of the page concerned to inclusion items, based on the templatelinks table.
    • Some extensions may perform some action during a subevaluation, e.g. with VariablesExtension a subevaluation may involve assignment of a value to a variable.

Thus {{ #ifexpr:.. | {{a|parameters}} | {{b |parameters}} }} evaluates {{a|parameters}} and {{b|parameters}} regardless of the condition. On the other hand, {{ {{#ifexpr:..|a|b}} | parameters }} just evaluates the expression for the template name (by evaluating the condition, and resulting in a or b) and the parameters, and the actually included template.

See also ParserFunctions#Code_execution.

Templates in different versions of MediaWiki

Version 1.6

  • More magic words and the parameter default mechanism were added.
  • <noinclude> and <includeonly> (see above) were added.

Version 1.5

Version 1.4

  • Including the same template more than five times in the same page works from version 1.4, now on all Wikimedia sites. Also new was that subst can be used with parameters.

Version 1.3

Version 1.2.6

  • In MediaWiki version 1.2.6, make a page MediaWiki:mytemplate and refer to it with {{msg:mytemplate}}. In this version parameters are not possible.
  • Compatibility with 1.3, in the sense of having the content in Template:mytemplate, is possible with a redirect from MediaWiki:mytemplate to Template:mytemplate on the 1.2.6 project.

Revision history of pages containing templates

Pages in the stored page history consist of wikitext with possible references to templates and images. When viewing an old version of a page, these refer to the current versions of the templates and images, if these still exist. Thus the former composite page is not reconstructed.

Other pages not primarily meant for direct viewing

Pages not primarily meant for direct viewing include, apart from templates:

See also

  1. arg: - instead of including a page for given parameter values, this allows linking to a page for given parameter values. Syntax for parameter use: {{arg:parameter name|default}}. Syntax for linking: external link style with "&parameter name=parameter value" added to the URL.

See also Meta:List of templates.

Links to other help pages

Help contents
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · Wikidata · MediaWiki · Wikibooks · Wikisource · MediaWiki: Manual · Google
Versions of this help page (for other languages see further)
What links here on Meta or from Meta · Wikipedia · MediaWiki
Reading
Go · Search · Stop words · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
Tracking changes
Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
Logging in and preferences
Logging in · Preferences · User style
Editing
Starting a new page · Advanced editing · Editing FAQ · Edit toolbar · Export · Import · Shortcuts · Edit conflict · Page size
Referencing
Links · URL · Piped links · Interwiki linking · Footnotes
Style and formatting
Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · List · Table · Sorting · Colors · Images and file uploads
Fixing mistakes
Show preview · Testing · Reverting edits
Advanced functioning
Expansion · Template · Advanced templates · Parser function · Parameter default · Variable · System message · Substitution · Array · Calculation · Embed page
Others
Special characters · Renaming (moving) a page · Preparing a page for translation · Talk page · Signatures · Sandbox · Legal issues for editors