Aiuto:Substitution

(Differenze fra le revisioni)
m (Reverted 1 edit by 169.139.112.129 (talk) identified as vandalism to last revision by Anomie. (TW))
(Creata pagina con 'bla')
Riga 1: Riga 1:
{{for|detailed guidance on when to use substitution rather than transclusion|Wikipedia:Substitution}}
+
bla
{{shortcut|H:SUBST}}
+
'''Substitution''' is a function whereby, when an editor saves a page, an item in the wikicode is permanently replaced with its current value. In particular, a [[Help:Template|template]] can be substituted, as an alternative to [[WP:transclusion|transclusion]]. The difference is that when a template is substituted on a page, its appearance on that page will no longer be affected by later changes made to the template itself.
+
 
+
Substitution is done using the <code>subst:</code> modifier after the double opening braces. For example, to substitute the template {{tl|afd}}, type <code><nowiki>{{subst:afd}}</nowiki></code>. You can check the resulting wikitext before saving by clicking "Show changes" (and see what the page will look like by clicking "Show preview").
+
 
+
[[Help:variable|Variable]]s and [[Help:parser function|parser function]]s can also be substituted, meaning that their current value will be recorded permanently on the page – they will not be re-evaluated whenever someone views the page.
+
 
+
==When to use substitution==
+
Generally speaking, templates are [[WP:transclusion|transcluded]] rather than substituted, although there are some that are specifically designed to be substituted (and may not work if transcluded). Some reasons for substituting templates include:
+
*making the resulting page independent of future changes to the template
+
*enabling other automatic replacements to occur (for example, a template may contain four tildes, which will be replaced by the user's signature and timestamp provided the template is substituted)
+
*enabling the page to be copied to another project without copying the template
+
*making page rendering faster for the server
+
*making the correspondence between wikitext and rendered page easier to understand (the opposite may also apply)
+
 
+
Disadvantages of substitution may include:
+
*Updates to the template will not be reflected on the target page
+
*The resulting wikitext is longer and more complicated
+
*Other users cannot see that the text was produced using a template (and will not learn to use the template)
+
 
+
Time-dependent variables are substituted to make the rendered page independent of the time at which it is viewed. Substituting page-dependent variables makes the resulting wikitext independent of renaming of the page and of copying to another page.
+
 
+
For more detailed guidance, including lists of templates which should or should not be substituted, see [[Wikipedia:Substitution]].
+
 
+
==Syntax==
+
To substitute a template, use the same syntax as for [[WP:transclusion|transclusion]], but include <code>subst:</code> after the opening double braces. For example, to substitute the template {{tl|afd}}, type <code><nowiki>{{subst:afd}}</nowiki></code>. Parameters can be included as in the case of transclusion, for example <code><nowiki>{{subst:afd|Some article}}</nowiki></code>.
+
 
+
Pages not in the template namespace can also be substituted, e.g. <code><nowiki>{{subst:User:Cleverclogs/My box}}</nowiki></code>. To substitute the current content of a page in [[WP:mainspace|mainspace]] (i.e. one with no namespace prefix), two colons are required, as in <code><nowiki>{{subst::Page}}</nowiki></code>. If a substituted page or template does not exist, the entire expression (with braces and subst:) will remain unchanged in the wikitext and on the page.
+
 
+
Use the same syntax to substitute variables and parser functions, e.g. <code><nowiki>{{subst:PAGENAME}}</nowiki></code> or <code><nowiki>{{subst:#switch:{{NUMBEROFADMINS}}|1=Foo|1000=Car|#default=Baz}}</nowiki></code>.
+
 
+
There may be whitespace between the opening braces and the "subst:". However it is recommended not to have any space or newlines immediately after the "subst:", as this will not work in every case.
+
 
+
==Technical implementation==
+
Substitution is part of the process performed on saving a page, and causes the wikitext saved as a result of the edit to differ from the text which the user actually entered in the edit window. Other replacements performed during this process include expansion of links using the [[WP:pipe trick|pipe trick]], and replacement of multiple tildes with signatures and timestamps.
+
 
+
This means that substitution necessarily occurs before any actions performed at the time of page rendering (conversion of the stored wikitext to HTML). In particular, ''substitutions are done before transclusions''. So typing <code><nowiki>{{subst:Help:L{{tc}}k}}</nowiki></code> will not do substitution, even though {{tl|tc}} returns the text "in" and [[Help:Link]] exists. The transclusion of {{tl|tc}} has not yet taken place when the substitution is attempted. However, replace <code><nowiki>{{tc}}</nowiki></code> with <code><nowiki>{{subst:tc}}</nowiki></code>, and both substitutions will be performed in the expected order.
+
 
+
Similarly:
+
*Typing <code><nowiki>{{subst:#if:{{x0}}|yes|no}}</nowiki></code> gives the wikitext "yes", even though {{tl|x0}} is an empty template, since the conditional parameter evaluates to the non-empty string "<nowiki>{{x0}}</nowiki>". However <code><nowiki>{{subst:#if:{{subst:x0}}|yes|no}}</nowiki></code> produces "no", since the inner substitution is performed first.
+
*Typing <code><nowiki>{{subst:#expr:2*{{{p|3}}}}}</nowiki></code> returns ''Expression error: unrecognised punctuation character "{"'', as the undefined parameter <nowiki>{{{p}}}</nowiki> has not been replaced by its default value (3) when the substitution occurs.
+
*If {{tl|t6}} contains the text "t2|a", then <code><nowiki>{{ {{subst:t6}} }}</nowiki></code> will produce the wikitext "<nowiki>{{ t2|a }}</nowiki>", rendered as "{{t2|a}}" Contrast this with the behaviour of <code><nowiki>{{ {{t6}} }}</nowiki></code>, which is rendered as "{{ {{t6}} }}", because the parser does not reinterpret the pipe as a separator during the non-substituted expansion stage. (Similarly, <code><nowiki>{{subst:{{subst:t6}} }}</nowiki></code> gives the wikitext <code><nowiki>{{subst:t2|a }}</nowiki></code>, which only on the next edit will be substituted with {{t2|a}}.)
+
 
+
Clicking the "Show changes" button during editing shows the wikitext that will result ''after'' substitution and other immediate replacements are carried out. Clicking "Show preview" shows what the rendered page will look like after these replacements.
+
 
+
If a page substitutes itself (e.g. in the noinclude part of a template page) it substitutes the old version, as it was before the current edit.
+
 
+
If the expansion of a substituted template itself contains instances of the <code>subst:</code> syntax, then the substitutions are performed recursively. However ''substitution is not automatically recursive'' &ndash; if a substituted template contains ordinary transclusions or variables and parser functions which are not explicitly substituted, then these will not be substituted.
+
 
+
Note also that if instances of the <code>subst:</code> syntax appear in a template that is being ''transcluded'', then they will be rendered unchanged (as "<nowiki>{{subst:...}}</nowiki>"), since no substitution in wikitext is possible at the rendering stage. This feature can be exploited to control template behaviour (see below, [[#Making templates behave differently when transcluded or substituted|''Making templates behave differently when transcluded or substituted'']]). However, it may be inconvenient when a template is designed to be possible to transclude as well as substitute &ndash; in this case <code>safesubst:</code> can be used instead of <code>subst:</code> (see next section).
+
 
+
===The safesubst: modifier <span id="safesubst"/>===
+
{{shortcut|WP:SAFESUBST}}
+
The <code>subst:</code> modifier can be replaced by the alternative modifier <code>safesubst:</code>. The two have the same behaviour, except when they are encountered during non-substituted expansion (transclusion or direct viewing) of a template. In this situation, the code <code><nowiki>{{subst:...}}</nowiki></code> remains unparsed, whereas <code><nowiki>{{safesubst:...}}</nowiki></code> is treated as if no modifier were present (so the subtemplate is transcluded or the variable or parser function evaluated).
+
 
+
Hence the <code>safesubst:</code> modifier is used in the code of templates which are designed to produce recursive substitution when substituted, but are also intended to work when transcluded &ndash; or simply to be viewed directly. Using plain <code>subst:</code> in such templates would break in the case of transclusion (and possibly on direct viewing). For details on how to implement this (in particular, how to prevent the substitution from being performed as soon as the template code is saved), see the next section, [[#Recursive substitution|''Recursive substitution'']].
+
 
+
==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, then, 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><includeonly>subst:</includeonly></nowiki></code> in place of plain <code>subst:</code>. The includeonly tags break up the substitution syntax when the template is saved, but those tags will be stripped away when it is later substituted, allowing the inner substitution to take effect.
+
*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:t|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>.
+
 
+
'''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]], {{tim|Example table with computations, with optional substitution}}, and the feature request at [[mediazilla:2777|substall]].
+
 
+
=== Examples ===
+
{{tn|!}} substitutes to |.
+
{| class="wikitable"
+
|-
+
! Contents of {{Tlf|foo}}.
+
! When you open <nowiki>[[Template:foo]]</nowiki>, it looks:
+
! The result of <nowiki>{{subst:foo}}</nowiki>.
+
|-
+
|<nowiki>{{!}}</nowiki><br />
+
<nowiki>{{PAGENAME}}</nowiki><br />
+
<nowiki>{{#if: 1 | yes | no}}</nowiki>
+
|&#124;<br />
+
Template:foo<br />
+
yes
+
|<nowiki>{{!}}</nowiki><br />
+
<nowiki>{{PAGENAME}}</nowiki><br />
+
<nowiki>{{#if: 1 | yes | no}}</nowiki>
+
|-
+
|<nowiki>{{{{{|subst:}}}!}}</nowiki><br />
+
<nowiki>{{{{{|subst:}}}PAGENAME}}</nowiki><br />
+
<nowiki>{{{{{|subst:}}}#if: 1 | yes | no}}</nowiki>
+
|<nowiki>{{subst:!}}</nowiki><br />
+
<nowiki>{{subst:PAGENAME}}</nowiki><br />
+
<nowiki>{{subst:#if: 1 | yes | no}}</nowiki><br />
+
|&#124;<br />
+
Template:foo<br />
+
yes<br />
+
|-
+
|<nowiki>{{{{{|safesubst:}}}!}}</nowiki><br />
+
<nowiki>{{{{{|safesubst:}}}PAGENAME}}</nowiki><br />
+
<nowiki>{{{{{|safesubst:}}}#if: 1 | yes | no}}</nowiki><br />
+
|&#124;<br />
+
Template:foo<br />
+
yes<br />
+
|&#124;<br />
+
Template:foo<br />
+
yes<br />
+
|}
+
 
+
==Making templates behave differently when transcluded or substituted==
+
Sometimes it is desirable to make a template behave differently when substituted than when transcluded. A common trick for doing this is to use an expression like <code><nowiki>{{{{{subst|subst:}}}ns:0}}</nowiki></code>. This evaluates to the empty string if the template is being substituted (since the inner substitution takes effect, giving the prefix of [[WP:namespace|namespace]] 0, which is empty), but to "<nowiki>{{subst:ns:0}}</nowiki>" if the template is being transcluded (since substitution cannot take place after transclusion).
+
 
+
A common application is in templates which are designed only to be substituted, to make them produce a warning if they are mistakenly transcluded instead. This is done in templates like {{tl|prod}}, which are designed to produce a timestamp (e.g. for adding pages to dated categories), and will not be able to do this if transcluded.
+
 
+
A template, {{tl|issubst}}, has been created to simplify this. It returns "yes" if substitution is being performed, or the empty string otherwise. This can then be used as the parameter of a conditional parser function to make the template display a warning method if it is being transcluded, or to otherwise change the behaviour of a template depending on whether it is being transcluded or substituted.
+
 
+
==Documenting substitution==
+
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. It can even contain the values of the parameters, because substitution of parameters works even in comments.
+
 
+
== Templates for substitution ==
+
* {{tl|require subst}}, {{tl|subst check top}}, and {{tl|subst check bottom}} can wrap templates which require subst:
+
* {{tl|issubst}} - Returns "yes" when substituted.
+
* {{tl|ifsubst}} - Similar to <nowiki>#if:</nowiki> [[H:MW|magic word]], judging from substituted or not.
+
 
+
==See also==
+
* [[m:Help:Substitution]], a longer and more technical help page at Meta
+
* [[mw:Extension:ExpandTemplates]]
+
* [[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: <span class="plainlinks">[{{fullurl:Special:Contributions|limit=1&target={{urlencode:Example User}}}} last edit]</span></span>
+
** {{tim|page history}}
+
* [[Wikipedia:WikiProject Check Wikipedia#Template programming element|WikiProject Check Wikipedia#Template programming element]] lists articles where templates were substituted rather than transcluded.
+
 
+
[[Category:Wikipedia help]]
+
 
+
[[zh:Help:替换引用]]
+

Versione delle 23:03, 4 nov 2012

bla

Strumenti personali
Namespace
Varianti
Azioni
Navigazione
Comunicall
Rete Intelligente
TLC Software
Miscellanea
Varie
Strumenti