Transwiki:TScope/doc
このモジュールはウィキテンプレートにおけるプログラミングのための関数をいくつか提供しています。これらの関数の共通点はスコープ、すなわち変数や関数を参照できる範囲との関連です。
これらの関数はラッパー関数には使えず、もとの関数から直接呼び出す必要があります。これは本モジュールが「テンプレートの引数と、モジュールを呼び出したテンプレートにアクセスできる」というScribuntoの機能を利用しているからです。
関数を呼び出す
[編集]Each of these functions calls another template with a set of parameters formed by merging the set of parameters to the surrounding template with the set of parameters to the module invocation. This is useful if the surrounding template is meant to be a shell providing a modified form of the services of another, underlying template. The shell template doesn't have to explicitly pass along every parameter to the underlying template; in fact, the shell doesn't even have to know what all the parameters to the underlying template are, just the particular parameters that matter to the way it modifies the services. If the underlying template is later changed by, say, adding parameters, the implementation of the shell doesn't have to be touched unless the new parameters actually matter to the way the shell modifies the services (although the documentation of the shell might need to be changed if it duplicates explanations of parameters from the underlying template). See for example {{dialog/view}}.
The first unnamed parameter to the invocation names the template to call.
Named parameters to the invocation are passed into the call. Each named parameter to the surrounding template is passed into the call unless overridden by a same-named invocation parameter.
The functions differ from each other in how they handle unnamed parameters.
override
[編集]This function handles additional unnamed parameters to the invocation, after the first naming the template to call, in the same way as named parameters: each unnamed invocation parameter is passed into the call in its corresponding position (the first additional parameter becomes unnamed-parameter-1 to the call, etc.), while each unnamed parameter to the surrounding template is passed into the call unless overridden by an invocation parameter in corresponding position.
For example, if page Template:Foo contains invocation
{{#invoke:TScope|override|bar|inner|quux=inner}}
and foo is called elsewhere by {{foo|outer|baz=outer|quux=outer}}
, then the invocation would call Template:Bar with parameters 1=テンプレート:TScope/ex-override-1
, quux=テンプレート:TScope/ex-override-1
, and baz=テンプレート:TScope/ex-override-1
.
The first additional unnamed invocation parameter, after the name of the template to call, is passed into the call with index 1, the second additional unnamed invocation parameter is passed into the call with index 2, and so on. This means, however, that if the unnamed invocation parameters were passed explicitly by their integer names, the integer names specified to the invocation would be one more than the parameter numbers received by the called template. That is,
{{#invoke:TScope|override|bar|baz|5=quux}}
which is equivalent to
{{#invoke:TScope|override|1=bar|2=baz|5=quux}}
would call Template:Bar with parameters 1=テンプレート:TScope/ex-override-2
and 4=テンプレート:TScope/ex-override-3
.
prepend
[編集]This function handles unnamed parameters by passing them all into the call, first all additional unnamed parameters to the invocation and then all unnamed parameters to the surrounding template. The indices of unnamed surrounding-template parameters are increased by the largest call-index of any of the additional unnamed invocation parameters. For example, if page Template:Foo contains invocation
{{#invoke:TScope|prepend|bar|quux|baz}}
and foo is called elsewhere by {{foo|fnord}}
, then the invocation would call Template:Bar with parameters 1=テンプレート:TScope/ex-prepend-1
, 2=テンプレート:TScope/ex-prepend-1
, and 3=テンプレート:TScope/ex-prepend-1
.
Each set of unnamed parameters (invocation parameters, and surrounding-template parameters) is treated as a solid block of indices starting with 1, even if some slots within the block (even at the start of the block) are unoccupied. Recalling the note under function override regarding the off-by-one discrepancy between invocation-indices and call-indices, if page Template:Foo contains invocation
{{#invoke:TScope|prepend|bar|5=quux|3=baz}}
and foo is called elsewhere by {{foo|3=fnord}}
, then the invocation would call Template:Bar with parameters 2=テンプレート:TScope/ex-prepend-2
, 4=テンプレート:TScope/ex-prepend-2
, and 7=テンプレート:TScope/ex-prepend-2
.
append
[編集]As function prepend, except that rather than unnamed parameters to the surrounding template having their indices increased, instead unnamed parameters to the module invocation have their indices increased, by the largest index of any unnamed parameter to the surrounding template. For example, if Template:Foo contains invocation
{{#invoke:TScope|append|bar|quux|baz}}
and foo is called elsewhere by {{foo|fnord}}
, then the invocation would call Template:Bar with parameters 1=テンプレート:TScope/ex-append-1
, 2=テンプレート:TScope/ex-append-1
, and 3=テンプレート:TScope/ex-append-1
.
drop
[編集]As function override, except that only named parameters of the surrounding template are passed on to the called template; unnamed parameters are passed only if specified directly to the module invocation.
shiftLeft
[編集]Similar to function override, but the first unnamed parameter to the invocation after the name of the template to call is not a parameter to pass into the call, but instead is an integer by which to decrement the indices of unnamed parameters to the surrounding template, dropping any that decrease below 1. For example, if Template:Foo contains invocation
{{#invoke:TScope|shiftLeft|bar|1}}
and foo is called elsewhere by {{foo|baz|quux|fnord}}
, then the invocation would call Template:Bar with parameters 1=テンプレート:TScope/ex-shiftLeft-1
and 2=テンプレート:TScope/ex-shiftLeft-1
; while invocation
{{#invoke:TScope|shiftLeft|bar|-2|inner}}
would call Template:Bar with parameters 1=テンプレート:TScope/ex-shiftLeft-2
, 3=テンプレート:TScope/ex-shiftLeft-2
, 4=テンプレート:TScope/ex-shiftLeft-2
, and 5=テンプレート:TScope/ex-shiftLeft-2
.
map
[編集]This function calls a named template multiple times, using an interface somewhat similar to function override. As usual, the first unnamed parameter to the invocation names the template to call. If that is the only parameter to the invocation, the target template is called once for each unnamed parameter to the surrounding template, passing in that unnamed parameter as the first unnamed parameter to the call, and passing in all named parameters from the surrounding template; the results are concatenated in increasing order of the indices of the unnamed parameters. For example, if Template:Foo contains invocation
{{#invoke:TScope|map|bar}}
and foo is called elsewhere by {{foo|baz|quux|fnord}}
, then the invocation would call Template:Bar three times, with respectively parameter 1=テンプレート:TScope/compare
, 1=テンプレート:TScope/compare
, and 1=テンプレート:TScope/compare
.
Named parameters to the invocation override same-named parameters to the surrounding template. The optional second unnamed parameter to the invocation must be a non-negative integer indicating so many leading unnamed parameters to the surrounding template should be passed as later unnamed parameters to each call instead of being featured as first-unnamed to a call. For example, if Template:Foo contains invocation
{{#invoke:TScope|map|bar|2|quux=inner}}
and foo is called elsewhere by {{foo|one|two|three|four|five|baz=outer|quux=outer|fnord=outer}}
, then the invocation would call Template:Bar three times, with respectively 1=テンプレート:TScope/compare
, 1=テンプレート:TScope/compare
, 1=テンプレート:TScope/compare
, and each call would get 2=テンプレート:TScope/compare
, 3=テンプレート:TScope/compare
, baz=テンプレート:TScope/compare
, quux=テンプレート:TScope/compare
, fnord=テンプレート:TScope/compare
. Additional unnamed parameters to the invocation are also passed to each call, overriding any passed through from the surrounding template; thus, in the immediately preceding example, if the invocation were instead
{{#invoke:TScope|map|bar|2|inner|quux=inner}}
the calls would be the same except with 2=テンプレート:TScope/compare
instead of 2=one
.
static
[編集]この関数は本モジュールを呼び出したテンプレートの名前を返します(一方、マジックワードの{{PAGENAME}}
は表示されているページの名前を返します)。たとえば、「Quux」というページが閲覧されているとします。QuuxのページではBarというテンプレートが呼び出され、BarではFooというテンプレートが呼び出され、Fooでは本モジュールのstatic関数とマジックワードの{{PAGENAME}}
が呼び出されているとします。その場合、staticはTemplate:Foo
を返し、PAGENAMEはQuux
を返します。
echo
[編集]この関数はウィキテーブルを2つ返します。1つ目は本モジュールを呼び出すときに渡された引数の名前と値、2つ目は本モジュールを呼び出すテンプレートが呼び出されたときに渡された引数の名前と値が含まれます。いずれも引数がない場合はテーブルも省略されます。