Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help
Community portal
Encyc
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Help:Substitution
(section)
Help page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Recursive substitution== As mentioned above, substitution is not automatically recursive. So if a substituted template has code containing transclusions of other templates and parser functions, that code will be copied "as is" into the resulting wikitext β the transcluded items will not themselves be substituted. To make substitution work recursively, you must include the <code>subst:</code> syntax in the code of the calling template. However, you cannot do this by simply typing "subst:" within the template, as the substitution would then be performed as soon as the template is saved. There are two ways to work around this problem: *Use <code><nowiki>subst:<noinclude/></nowiki></code> in place of plain <code>subst:</code>. The noinclude tag breaks up the substitution syntax when the template is saved, but will be stripped away when it is later substituted, allowing the inner substitution to take effect. Alternatively, a larger part of the template code can be wrapped in {{tag|onlyinclude}}. Then the wrapped code is ignored on the template page itself but not when it is transcluded. *Make "subst:" the possible value of an expression containing a parameter, such as <code><nowiki>{{{subst-foo|subst:}}}</nowiki></code>, which will evaluate to "subst:" provided the parameter ''subst-foo'' is not set. This is a more flexible solution, as it allows the behaviour to be controlled via the parameter. For example, such a template might be called using <code><nowiki>{{subst:</nowiki>{{var|Templatename}}<nowiki>|subst-foo=|...}}</nowiki></code>, assigning the parameter an empty value and thus turning off the second level of substitution. If it is not planned to use the parameter, the parameter name is often chosen to be the empty string, giving <code><nowiki>{{{|subst:}}}</nowiki></code>, but this can have unexpected results if a value is assigned to the empty string parameter (e.g. <code><nowiki>{{Templatename|=foo}}</nowiki></code>). '''To ensure that the template will still work as intended if it is transcluded instead of substituted, use <code>safesubst:</code> instead of <code>subst:</code>.''' This also applies if the template is also to be viewed directly, on its own page (although in this case, if the first of the above methods is used, plain subst: will still work, as the includeonly tags will cause the parser to ignore the subst: on direct viewing). To see what a template will produce when fully expanded, without the need to explicitly substitute all subtemplates etc., the [[Special:ExpandTemplates]] tool can be used. For more information, see the [[m:Help:Recursive conversion of wikitext|Recursive conversion of wikitext]] help page at Meta. See also [[Help:Calculation#Substitution|Help:Calculation Β§ Substitution]], {{tim|Example table with computations, with optional substitution}}, and the feature request at [[mediazilla:2777|substall]]. ===Recursive substitution in guided tours=== [[Help:Guided tours|Guided tours]] can make posts on behalf of users, such as automatically posting the contents of a wiki page onto a talk page. Unlike normal substitution or transclusion, however, this function of guided tours does not respect {{tag|includeonly}} and similar markup. To make recursive substitution work via guided tour posting, you can use the [[mw:Manual:Recursive conversion of wikitext#Delaying substitution with Template:subst|Delaying substitution with Template:subst]] method. === An example of recursion in action === This is an example using the actual [[Template:Like]], and a hypothetical Template:Foo. You are attempting to include the Template:Foo, and indirectly the Template:Like, on a third page through transclusion and substitution. Template:Like displays this symbol: {{Like}} For example, let's say the Template:Foo contained the wikitext <code><nowiki>{{Like}}</nowiki></code>. If you then transclude it, using the wikitext <code><nowiki>{{Foo}}</nowiki></code>, this will transclude the Template:Like as you would expect. But if you use the wikitext <code><nowiki>{{subst:Foo}}</nowiki></code>, when you save it, you find the wikitext <code><nowiki>{{Like}}</nowiki></code> substituted rather than the wikitext of the page Template:Like. The substitution has not been recursive. An example of recursion would be if the Template:Foo contained the wikitext <code><nowiki>{{{{{|safesubst:}}}Like}}</nowiki></code>. Now if you transclude it, using the wikitext <code><nowiki>{{Foo}}</nowiki></code>, this will transclude the Template:Like just as before. And if you save <code><nowiki>{{subst:Foo}}</nowiki></code> it will substitute the full actual wikitext of the Template:Like. You have therefore achieved recursive substitution. === Further examples === Note: {{Tlf|!}} substitutes to |. {| class="wikitable" |- ! rowspan="2" | Wikitext of Template:Foo ! rowspan="2" | When you view <nowiki>Template:Foo</nowiki>,<br />it looks like: ! colspan="2" | The result of <nowiki>{{Foo}}</nowiki><br />if included on this page ! colspan="2" | The result of <nowiki>{{subst:Foo}}</nowiki><br />if included on this page |- ! saves as: ! renders as: ! saves as: ! renders as: |- | <nowiki>{{!}} {{PAGENAME}} {{#if: 1 | yes | no}}</nowiki> || Foo yes | <nowiki>{{Foo}}</nowiki> || Substitution yes | <nowiki>{{!}} {{PAGENAME}} {{#if: 1 | yes | no}}</nowiki> || Substitution yes |- | <nowiki>{{{{{|subst:}}}!}} {{{{{|subst:}}}PAGENAME}} {{{{{|subst:}}}#if: 1 | yes | no}}</nowiki> |<nowiki>{{subst:!}}</nowiki> <nowiki>{{subst:PAGENAME}}</nowiki> <nowiki>{{subst:#if: 1 | yes | no}}</nowiki> | <nowiki>{{Foo}}</nowiki> |<nowiki>{{subst:!}}</nowiki> <nowiki>{{subst:PAGENAME}}</nowiki> <nowiki>{{subst:#if: 1 | yes | no}}</nowiki> | <nowiki>| Substitution yes</nowiki> || Substitution yes |- | <nowiki>{{{{{|safesubst:}}}!}} {{{{{|safesubst:}}}PAGENAME}} {{{{{|safesubst:}}}#if: 1 | yes | no}}</nowiki> || Foo yes | <nowiki>{{Foo}}</nowiki> || Substitution yes | <nowiki>| Substitution yes</nowiki> || Substitution yes |}
Summary:
Please note that all contributions to Encyc are considered to be released under the Creative Commons Attribution-Share Alike License (see
Encyc:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
This page is a member of a hidden category:
Category:Pages with short description
Toggle limited content width