Help:Subst展開

From mediawiki.org
This page is a translated version of the page Help:Substitution and the translation is 39% complete.
PD 注意: このページを編集すると、編集内容が CC0 のもとで公開されることに同意したと見なされます。詳細はパブリック・ドメインのヘルプ ページを参照してください。
このページの古い版の一部は、CC BY-SA ライセンスのもとで取り込まれました。 新しい編集のみがパブリックドメインと見なされます。
PD

subst 展開 (substitution、置換) は、ページの保存時にウィキテキストが 1 つ以上のテンプレート変数パーサー関数を参照している場合に実行される、ウィキテキストの自動変換です。

対象がテンプレートの場合、引数の値を代入後、テンプレートの呼び出し文字列を当該テンプレートの内容自体に変換します。 テンプレートは文字列を処理するマクロとして機能し、通常この処理はページの閲覧時に行われますが、subst展開を使用すると、当該処理をページの保存時に行うことができます。

変数やパーサー関数の subst 展開の場合、その部分への参照は処理結果の値に置換されます。

subst展開は、マジックワードsubst:またはsafesubst:を、それぞれ{{subst:FULLPAGENAME}}{{safesubst:FULLPAGENAME}}のように、(スペースなしの) 二重波括弧の次に配置することで使用できます。 safesubst:は、マルチレベルのsubst展開が必要な際に便利です。詳しくは以下を参照してください。

入力した文字列とsubst展開された文字列の内容は、ページの保存前にも編集画面上の[差分を表示]から確認できます。 ただし、テキストが複数の段落にまたがっている場合、差分に表示される内容には余白にプラス記号がある都合上、(変換後の文字列の一部を使用したい場合などに) そのままコピーするのにはあまり適していないことに注意してください。

機能

テンプレートのsubst展開:

  • 読み込み側のページは元テンプレートに依存しなくなります。
    • 元テンプレートが編集されても、読み込み側のページの内容は変化しません。
    • 元テンプレートを移入せずとも、読み込み側のページの内容を別のウィキにコピーできます。
  • ページの表示 (レンダリング) への影響が低く、サーバー側の処理がより速くなります。
  • テンプレートのもともとの機能を解析・反映します。 ただし、場合によってsubst展開後の表示結果は異なる場合があります。
  • ウィキテキストとページの表示内容間の対応関係が把握しやすくなります。(ただし、逆に難しくなる場合もあります。)

時刻関連の変数のsubst展開:

  • 読み込み側のページは表示時の時刻に依存しなくなります。

ページに依存する変数のsubst展開:

  • ページの改名および内容のコピー・ペースト時、値が動的に変化しなくなります。(ただし、PAGENAME 変数がnoinclude処理されている場合はこの限りではありません。)

一部の MediaWiki 拡張機能には、テンプレートにパラメーターを使用している場合、テンプレートが subst 置換されている場合にのみ機能するという制限があります。

概要

subst展開の解説において、「通常のテンプレート」 は、{{subst:ページ名 }} (テンプレート名前空間のページ用) または {{subst:完全なページ名 }} (その他の名前空間のページ用) のいずれかのパターンにおけるページ名指定部分を指し、 定義済みテンプレートは、同様に置換される変数またはパーサー関数です。

subst 展開は、(subst 展開されていない) テンプレート、変数、パーサー関数、パラメーターの展開の前に実行される、独立したプロセスです。

subst展開された要素の内部には、別のsubst展開された要素を埋め込むことができます。 同様に、subst展開されるテンプレートに別のsubst展開をネストできます。 テンプレートやパーサー関数の名前の式、展開されたテンプレートやパーサー関数のパラメーター定義、および展開されたテンプレートの本文の subst 展開が最初に実行されます。

その他の展開処理は後に実行されるため、subst展開対象の文字列が二重波括弧を含んでいる場合、当該二重波括弧は通常のテキストとして処理されます。 これにより、二重波括弧を含む引数名 (例:{{subst:foo|a{{bc}}d=...}}) と、テンプレートボディ内で使われうる同じ名前のパラメータ (例:{{{a{{bc}}d}}}) は共起できます。

存在しないテンプレートなどをsubst展開しようとした場合、展開は行われず、「subst:」の接頭辞のみがウィキテキストに残ります。

subst展開の処理後、テンプレートやその他のウィキテキストのレンダリングが通常通り行われます。 ただし、この通常処理の際にはsubst展開処理は既に完了しているため、subst展開時に検知された引数の名称不一致などは自動修正されません (後述の「部分展開」も参照)。

subst展開は、以下の項目が完全に評価可能な場合にのみ動作します。

  • テンプレート、変数、パーサー関数の名称
  • 対象がテンプレートの場合: テンプレートの呼び出し文およびテンプレート自体に含まれる引数名
  • 対象が#if, #ifexpr, #ifexist, #iferrorの場合: コロンに後続するパラメータ
  • 対象が#ifeqの場合: コロンに後続するパラメータおよびその次のパラメータ
  • 対象が#switchの場合: コロンに後続するパラメータおよび等号「=」の左側の記述

上述の通りsubst展開はその他の展開処理の前に行われますが、展開対象の表現がsubst処理を経ない場合、「その他の展開処理」の実行時に上記の評価はされていない状態となります。

ここで言及していない他のパーサー関数の場合でも、コロンの後に完全に評価されないパラメーターがある場合、パーサー関数は展開後のウィキテキストではなく括弧内のウィキテキストに適用され、その結果に影響を与えます。

例:

  • Template:tcを用いた{{subst:Help:L{{tc}}k}}はsubst展開されません。これは、Help:L{{tc}}kはHelp:Linkとしてレンダリングされる一方、Help:L{{tc}}kというページ自体は存在しないためです。 この結果、生成されるウィキテキストはもともとのウィキテキストと同じとなり、{{subst:Help:Link}}としてレンダリングされます。
  • Template:voidを用いた{{#if:{{void|abc}}|yes|no}}は「no」とレンダリングされるため、{{subst:#if:{{subst:void|abc}}|yes|no}}も同様に「no」とレンダリングされます。 一方、Template:voidはsubst展開処理が終わるまでは処理されないため、{{subst:#if:{{void|abc}}|yes|no}}は「yes」とレンダリングされます。

原則として、subst展開を経たウィキテキストは、当該処理後は通常のウィキテキストと同様に処理・表示されます。

ただし、パーサー関数の subst 展開では、パラメーターの値で使用される既定値が指定されていない場合、その既定値に置換されません。例えば、#expr の subst 展開時に評価される数値式では、これが許可されません:

  • {{#expr:2*{{{p|3}}}}} は 6 を返しますが、{{subst:#expr:2*{{{p|3}}}}} は以下を返します: Expression error: unrecognised punctuation character "{"

On substitution of a template that contains this, {{{p|3}}} is either replaced by the value of {{{p}}} or by 3, so then there is no complication.

内容が「t2demo|a<noinclude>[[Category:Demo templates]]</noinclude>」の Template:t6 を使用した {{ {{t6}} }} は、{{ t2demo|a }} のようにレンダリングされます。 {{subst:{{subst:t6}} }} はウィキテキスト {{subst:t2demo|a }} を同じようにレンダリングし、次の編集/保存で start-a -middle-{{{2}}}-end に変更されます。 {{ {{subst:t6}} }} はウィキテキスト {{ t2demo|a }} を返し、start-a -middle-{{{2}}}-end のようにレンダリングされます。 これは、subst 展開を行わない場合と完全な subst 展開の場合の両方で、テンプレート呼び出し内のパイプ文字が、内部テンプレート呼び出し、テンプレート パラメーター、リンク、画像タグを除いて、パラメーター定義同士やテンプレート名からの区切りを決定するからです。 この区切りは、テンプレート名とパラメーター定義の展開形式に追加のパイプ文字があるかどうかに依存しません。 しかし、内部テンプレートの subst 展開後にパイプ文字が外部テンプレート呼び出しにある場合、それは他のどのパイプ文字とも同様に扱われ、区切りを決定する役割を果たします。 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 {{{p|q}}} (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}} (in English Wikipedia), {{subst:t pd}} gives the wikitext 2. If a page substitutes itself (e.g. in the noinclude-part of a template page) it substitutes the old version.

考慮すべき点

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. {{#expr:2*3}} 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 source code and/or the data to produce new results, and we do not directly change the object file, here we would change the template calls and/or the templates, instead of changing the wikitext resulting from substitution directly.

記事テンプレート

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.

例: m:Template:t2, containing
start-{{{1}}}-middle-{{{2}}}-end
and called as {{subst:t2|[[a]]|{{tc}}}} (see {{Tc }}) gives the wikitext:
start-[[a]]-middle-{{tc}}-end, rendering as
start-a-middle-in-end.

Substitution removes the noinclude parts and the includeonly tags.

パラメーター:

  • A substitution with p=r replaces {{{p}}} and {{{p|q}}} by r. This includes the cases that r is of the form {{{s}}} or {{{s|t}}}.
  • A substitution with undefined p preserves {{{p}}} and replaces {{{p|q}}} by the default q (in English Wikipedia).

With subst: the replacement of a template tag by wikitext does not work recursively. For full recursive substitution use Special:ExpandTemplates. See also substall, and 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 {{ msgnw:pagename }} inclusion. However, template substitution excludes ‎<noinclude> parts, removes ‎<includeonly> tags, and replaces undefined parameters with defaults by those defaults.

事前定義されたテンプレート

In the case of substituting a predefined template, without parameters depending on other templates, the tag is replaced by the result.

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

{{subst:CURRENTYEAR}}-{{subst:CURRENTMONTH}}-{{subst:CURRENTDAY}} T {{subst:CURRENTTIME}} [[w:UTC|]]

may give the wikitext

2010-04-10 T 06:30 [[w:UTC|UTC]]

rendered as

2010-04-10 T 06:30 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 subst: modifier, otherwise the result is undefined.

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

However:

  1. {{subst:UC:{{tc}}}} - gives the wikitext {{TC}} rendered as Template:TC.
  2. {{subst:ns:{{#expr:2*3}}}} - stays {{subst:ns:{{#expr:2*3}}}}, rendered as {{subst:ns:6}}.

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 x: in a nested {{ x:...{{ y:...}} }} often is only suitable if all inner y: are also substituted.

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

Examples:

  • {{#expr:2*{{{p}}}}}Expression error: Unrecognized punctuation character "{".
  • {{#expr:2*{{{p|3}}}}}6
  • {{subst:#expr:2*{{{p|3}}}}}Expression error: Unrecognized punctuation character "{".
  • substituting a template containing {{<includeonly>subst:</includeonly>#expr:2*{{{p|3}}}}} gives 6 if p is not assigned a value, and twice the number p if it is assigned a value.
  • the same result can be obtained from a template containing {{{{{subst}}}#expr:2*{{{p|3}}}}}, if the substitution call has a parameter of "subst=subst:".

Compare:

  • {{uc:2*{{{p}}}}}2*{{{P}}}
  • {{uc:2*{{{p|q}}}}}2*Q
  • {{subst:uc:2*{{{p|q}}}}}the wikitext 2*{{{P|Q}}} rendered as 2*Q

and also (from above):

  • {{subst:UC:{{subst:tc}}}} - gives IN, just like {{UC:{{tc}}}} does; UC is applied to the output "in" of Tc.
  • {{subst:UC:{{tc}}}} - gives the wikitext {{TC}} rendered as Template:TC.

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

部分展開

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

  • {{subst:t}} - gives the wikitext start-{{{1|pqr}}}-end, just that of m:Template:t, without noinclude parts and includeonly tags
  • {{subst:t|a{{{p|q}}}b}} - gives the wikitext start-a{{{p|q}}}b-end

Examples with double substitution:

  • {{subst:3X|{{subst:t}}}} - gives the wikitext start-{{{1|pqr}}}-endstart-{{{1|pqr}}}-endstart-{{{1|pqr}}}-endstart-{{{1|q}}}-end
  • {{subst:3X|{{subst:t|{{{1|q}}}}}}} - gives the wikitext start-{{{1|q}}}-endstart-{{{1|q}}}-endstart-{{{1|q}}}-end

多重subst展開

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), this code is provided as default value of an 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 unused parameter, so we can use the code {{{|safesubst:}}}.

The difference with {{{|subst:}}} is that {{{|safesubst:}}}, evaluating to safesubst: 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. To make the template such that it allows the choice between these two options as well as one-level substitution (and more choices if more templates, variables, and/or parser functions are involved) one or more parameters are needed, see below.

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 template tag lay-out . This would affect the working of the code {{{|safesubst:}}}. To allow this other dummy use of the parameter, another parameter name can be used in {{{parameter name|safesubst:}}}, or to avoid any possible clash of dummy parameter names, includeonly tags can be used, see below.

Multilevel substitution with independent control of each substitution separately

A parameter subst (or more, each with its own name) can be used with safesubst:" and the empty string as possible values. Thus we can for example control whether an inner template is substituted too when the outer template is substituted. Either possibility 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.

Similarly, if there are multiple templates, variables, and/or parser functions in the inner template(s) we can control substitution of all, either independently by using different parameters, or with some or all using the same parameter.

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 {{{subst1|}}}; for calling T we can use:
    • {{t|..}} - no substitution
    • {{subst:t|..}} - one-level substitution
    • {{subst:t|subst1=subst:|..}} - 2 段階 subst 展開
    • {{subst:t|subst1=safesubst:|..}} - ditto
  • with default "safesubst:", T calls inner templates and parser functions prefixing their names with {{{subst1|safesubst:}}}; for calling T we can use:
    • {{t|..}} - subst展開なし
    • {{subst:t|subst1=|..}} - one-level substitution
    • {{subst:t|..}} - 2 段階 subst 展開

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={{{subst1|}}} or subst2={{{subst1|safesubst:}}}, respectively (variables and parser functions don't get the additional parameter).

部分的なsubst展開

Using a template prepared for optional subst=subst: only with ordinary substitution, without specifying parameter values, allows to insert its code into another template, like copy and paste, but all ‎<noinclude> parts and ‎<includeonly> 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 #switch: like m:Template:len:

  1. Development code:
    {{#switch: {{len|parameter tag}}|0=case 0 etc.}}
  2. Standard solution:
    {{{{{subst|}}}#switch: {{{{{subst|}}}len|parameter tag|subst={{{subst|}}}}}|0=case 0 etc.}}
  3. Better solution: create template code by applying substitution using this wikitext:
    {{{{{subst|}}}#switch: {{subst:len|parameter tag}}|0=case 0 etc.}}

m:Template:len is prepared for optional substitution, therefore both solutions work, but the latter solution substituting its code is simpler and more efficient.

See m:Template:csn and m:Template:lz12 for cases where Template:len 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. m:Template:ts1.

Composite operations

By {{A|{{B|p}}}} 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 {{C|p}} of a "composite template" C with parameter p.

The wikitext for template C would be {{A|{{B|{{{1}}}}}}}, or with optional substitution the following construct :
{{ {{{subst|}}} A|{{ {{{subst|}}} B|{{{1}}} |subst={{{subst|}}} }} |subst={{{subst|}}} }}
The subst={{{subst|}}} is only necessary for recursive substitution as explained above.

Note that it is not useful to specify {{subst|subst:}} 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={{{subst|}}} for that template.


includeonly

An alternative method to prevent premature substitution, known as "includeonly subst magic", 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 {{<includeonly>safesubst:</includeonly>something}} 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 Extension:InputBox and "substitution" in Help:Magic words .

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 w:Template:en. This technique is essential for templates like w:Template:en 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 subst=subst: is in effect:
{{{{{subst|}}}ifdef|{{{{{subst|subst:}}}ns:0}}|'''Warning'''}}.
  1. Output for {{T}} or {{subst:T}} - Warning
  2. Output for {{T|subst=subst:}} - Template:Ifdef
  3. Output for {{subst:T|subst=subst:}} - nothing (no remaining wikitext)
This is a rare case where replacing ifdef by #if: doesn't work directly.

Substitution of part of the parameters

Let template Feelings use parameters 1 and 2. Consider creating a template Emotions with one parameter 1, corresponding to Feelings, with a given value love of parameter 2. Compare {{Feelings|2=love}} and {{Feelings|1={{{1}}}|2=love}}. They look the same on the template page, see e.g. m:Template:t ps, but the first does not work because {{{1}}} is treated as text, not as parameter.

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 Feelings contains e.g. #expr with an expression containing both parameters the same applies, except that we can only substitute the highest level (Feelings), 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:

  • Feelings template containing With {{{1}}} one can {{{2}}}
    • 1=love2=help で置換すると、With love one can help になります。
    • 2=help で置換すると、With {{{1}}} one can help になります。 さらに、1=compassion で置換すると、With compassion one can help になります。
  • {{#if:{{{4}}}|{{{3}}}p}} を含むテンプレートの 2 段階 subst 展開。
    • 3=u4=v で置換すると、up になります。
    • 4=v で置換すると、{{{3}}}p になります。 さらに、3=u で置換すると、up になります。

Examples without equality:

  • {{#if:{{{3}}}|{{{4}}}p}} を含むテンプレートの 2 段階 subst 展開
    • 3=4=v で置換すると、空文字列になります。
    • 4=v で置換すると、vp になります。 さらに、3=u で置換すると、vp のままです。
  • {{#if:{{{2}}}|{{{1}}}p}} を含むテンプレートの 2 段階 subst 展開
    • 1=u2=v で置換すると、up になります。
    • 2=v で置換すると、{{{1}}}pp になります (バグ)。 さらに、3=u で置換すると、upp になります。
  • {{#expr:{{{1}}}*{{{2}}}}} を含むテンプレートの 2 段階 subst 展開
    • 1=72=8 で置換すると、56 になります。
    • 2=8 で置換すると、Expression error: Unrecognised punctuation character "{" になります。 さらに、1=7 で置換すると、同じ結果です。

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 {{{1{{{{{substvoid|}}}void}}}}} 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:

  • With {{{1|love}}} one can {{{2}}} を含むテンプレートを 2=help で 2 段階 subst 展開すると With {{{1|love}}} one can help になります。
  • {{#if:{{{4}}}|{{{3|d}}}p}} を含むテンプレートを 4=v で 2 段階 subst 展開すると dp になります。

Not rendered the same as without substitution:

  • {{#if:{{{3|}}}|{{{4}}}p}} を含むテンプレートを 4=v で 2 段階 subst 展開すると vp になります。
  • {{#if:{{{2}}}|{{{1|d}}}p}} を含むテンプレートを 2=v で 2 段階 subst 展開すると dpp になります (バグ)。
  • {{#expr:{{{1|6}}}*{{{2}}}}} を含むテンプレートを 2=8 で 2 段階 subst 展開すると以下を返します: Expression error: Unrecognised punctuation character "{"

After substitution with the parameter definition:

  • {{subst:#if:{{{3|}}}|vp}}vp
  • {{subst:#if:v|{{{1|d}}}p}}dpp (バグ)
  • {{subst:#expr:{{{1|6}}}*8}}Expression error: Unrecognised punctuation character "{"

Rewritten:

  • {{subst:#if:{{subst:#ifeq:{{{3|+}}}|{{{3|-}}}|vp}}}}the empty string
  • {{subst:#if:v|{{subst:#ifeq:{{{1|+}}}|{{{1|-}}}|{{{1}}}|d}}p}}dp
  • {{subst:#expr:{{subst:#ifeq:{{{1|+}}}|{{{1|-}}}|{{{1}}}|6}}*8}}48


制限

subst 展開は、‎<ref>...‎</ref>‎<gallery>...‎</gallery> のようなパーサー タグ内では使用できません。 また、{{subst:foo}}のように存在しないページを指定した場合、subst展開も参照読み込みもされず、文字列がそのまま表示されます。

subst展開の事後参照

subst:を用いてテンプレートを使用した場合、ページの変更履歴上からはその事実を確認できません。 これを理由とし、「subst:」を含む文言を編集要約に残すと事後参照時に役立ちます。

また、subst展開されたテンプレートを含むページはリンク元としては表示されず、編集画面においても当該テンプレートは参照読み込みテンプレート一覧に表示されません。 subst展開を追跡するために特定のカテゴリを設けることも可能ですが、このようなカテゴリはメインとなるカテゴリリストの煩雑化につながる恐れがあります。 また、(noinclude部を除き) コメント (<!---->) はウィキテキストとして残るため、 この中にテンプレート名を書き残しておく方法も考えられます。 この方法を用いた場合、パラメータのsubst展開はコメントの中でも機能するため、テンプレートの引数も書き記すことが可能です。

関連項目