「ヘルプ:Parser functions in templates」の版間の差分

提供: ひつじ小屋別館2代目
移動先: 案内検索
(Made the comparison table MUCH less confusing.)
 
(1版 をインポートしました)
 
(4人の利用者による、間の9版が非表示)
1行目: 1行目:
When applying [[Help:Extension:ParserFunctions|ParserFunctions]] to [[Help:Template|template]] parameters, a pipe symbol ("|") may be used to provide the empty string as default value, in order to indicate how to handle things if a parameter is or is not passed in.
+
<languages />
 +
{{TNT|PD Help Page}}
 +
<translate>
 +
<!--T:1-->
 +
When applying [[<tvar|ext>Special:MyLanguage/Help:Extension:ParserFunctions</>|ParserFunctions]] to [[<tvar|templates>Special:MyLanguage/Help:Templates</>|template]] parameters, a pipe symbol ("|") may be used to provide the empty string as default value, in order to indicate how to handle things if a parameter is not passed in.</translate>
 +
<translate>
 +
<!--T:23-->
 +
In the absence of this, an undefined parameter will not expand (i.e. will expand to <tvar|1><tt><nowiki>{{{1}}}</nowiki></tt></>), which is not an empty string, and thus true.
 +
</translate>
  
 
*<tt><nowiki>{{{1}}}</nowiki></tt>
 
*<tt><nowiki>{{{1}}}</nowiki></tt>
  
 +
<translate>
 +
<!--T:2-->
 
:;Sample A: <tt><nowiki>{{#if: {{{1}}} | Parameter 1 is not defined, or is defined and non-null/non-empty. | Parameter 1 is null. It contains only empty string(s) or breaking space(s) etc.}}</nowiki></tt>
 
:;Sample A: <tt><nowiki>{{#if: {{{1}}} | Parameter 1 is not defined, or is defined and non-null/non-empty. | Parameter 1 is null. It contains only empty string(s) or breaking space(s) etc.}}</nowiki></tt>
  
 +
<!--T:3-->
 
:;Result: <tt>{{#if: {{{1}}} | Parameter 1 is not defined, or is defined and non-null/non-empty. | Parameter 1 is null. It contains only empty string(s) or breaking space(s) etc.}}</tt>
 
:;Result: <tt>{{#if: {{{1}}} | Parameter 1 is not defined, or is defined and non-null/non-empty. | Parameter 1 is null. It contains only empty string(s) or breaking space(s) etc.}}</tt>
 +
</translate>
  
 
*<tt><nowiki>{{{1|}}}</nowiki></tt>
 
*<tt><nowiki>{{{1|}}}</nowiki></tt>
  
 +
<translate>
 +
<!--T:4-->
 
:;Sample B: <tt><nowiki>{{#if: {{{1|}}} | Parameter 1 is defined and non-null/non-empty. | Parameter 1 is not defined, or is defined but null. It contains only empty string(s) or breaking space(s) etc.}}</nowiki></tt>
 
:;Sample B: <tt><nowiki>{{#if: {{{1|}}} | Parameter 1 is defined and non-null/non-empty. | Parameter 1 is not defined, or is defined but null. It contains only empty string(s) or breaking space(s) etc.}}</nowiki></tt>
  
 +
<!--T:5-->
 
:;Result: <tt>{{#if: {{{1|}}} | Parameter 1 is defined and non-null/non-empty. | Parameter 1 is not defined, or is defined but null. It contains only empty string(s) or breaking space(s) etc.}}</tt>
 
:;Result: <tt>{{#if: {{{1|}}} | Parameter 1 is defined and non-null/non-empty. | Parameter 1 is not defined, or is defined but null. It contains only empty string(s) or breaking space(s) etc.}}</tt>
  
 +
<!--T:6-->
 
;Comparison
 
;Comparison
 +
</translate>
 
{| class="wikitable" width="90%"
 
{| class="wikitable" width="90%"
! Example of usage in a template<br />unnamed first parameter (1=)<br />named parameter (param=)
+
! <translate><!--T:7-->
! Condition of parameter
+
Example of usage in a template<br />unnamed first parameter (1=)<br />named parameter (param=)</translate>
! Sample A:<br /><nowiki>{{#if: {{{1}}}|</nowiki><span style="font-variant: small-caps;"> true</span><nowiki>|</nowiki><span style="font-variant: small-caps;"> false</span><nowiki>}}</nowiki><br /><nowiki>{{#if: {{{param}}}|</nowiki><span style="font-variant: small-caps;"> true</span><nowiki>|</nowiki><span style="font-variant: small-caps;"> false</span><nowiki>}}</nowiki>
+
! <translate><!--T:8-->
! Sample B:<br /><nowiki>{{#if: {{{1|}}}|</nowiki><span style="font-variant: small-caps;"> true</span><nowiki>|</nowiki><span style="font-variant: small-caps;"> false</span><nowiki>}}</nowiki><br /><nowiki>{{#if: {{{param|}}}|</nowiki><span style="font-variant: small-caps;"> true</span><nowiki>|</nowiki><span style="font-variant: small-caps;"> false</span><nowiki>}}</nowiki>
+
Condition of parameter</translate>
 +
! <translate><!--T:9-->
 +
Sample A:</translate><br /><nowiki>{{#if: {{{1}}}|</nowiki>True<nowiki>|</nowiki>False<nowiki>}}</nowiki><br /><nowiki>{{#if: {{{param}}}|</nowiki>True<nowiki>|</nowiki>False<nowiki>}}</nowiki>
 +
! <translate><!--T:10-->
 +
Sample B:</translate><br /><nowiki>{{#if: {{{1|}}}|</nowiki>True<nowiki>|</nowiki>False<nowiki>}}</nowiki><br /><nowiki>{{#if: {{{param|}}}|</nowiki>True<nowiki>|</nowiki>False<nowiki>}}</nowiki>
 
|-
 
|-
|<br /><code><nowiki>{{template}}</nowiki></code>||not defined:<br />parameter not used,<br />important for named parameters||style="background-color: #AAFFAA;" align="center"| '''TRUE'''||style="background-color: #FFAAAA;" align="center"| ''FALSE''
+
|<br /><code><nowiki>{{template}}</nowiki></code>||<translate><!--T:11-->
 +
not defined:<br />parameter not used,<br />important for named parameters</translate>||style="background-color:#AFA" align="center"|'''True'''||style="background-color:#FAA" align="center"| ''False''
 
|-
 
|-
|<br /><code><nowiki>{{template|}} .. {{template|1=}} .. {{template|param=}}</nowiki></code>||defined, but null/empty||style="background-color: #FFAAAA;" align="center"| ''FALSE''||style="background-color: #FFAAAA;" align="center"| ''FALSE''
+
|<br /><code><nowiki>{{template|}} .. {{template|1=}} .. {{template|param=}}</nowiki></code>||<translate><!--T:12-->
 +
defined, but null/empty</translate>||style="background-color:#FAA" align="center"| ''False''||style="background-color:#FAA" align="center"| ''False''
 
|-
 
|-
|<br /><code><nowiki>{{template|value}} .. {{template|1=value}} .. {{template|param=value}}</nowiki></code>||defined<br />and<br />NOT null neither empty||style="background-color: #AAFFAA;" align="center"| '''TRUE'''||style="background-color: #AAFFAA;" align="center"| '''TRUE'''
+
|<br /><code><nowiki>{{template|value}} .. {{template|1=value}} .. {{template|param=value}}</nowiki></code>||<translate><!--T:13-->
 +
defined<br />and<br />NOT null neither empty</translate>||style="background-color:#AFA" align="center"| '''True'''||style="background-color:#AFA" align="center"| '''True'''
 
|}
 
|}
  
 +
<translate>
 +
<!--T:14-->
 
The second usage (<tt><nowiki>{{{1|}}}</nowiki></tt>, sample B) with present empty default is often the desired way to handle situations where a parameter exists, but is comprised only of empty space.
 
The second usage (<tt><nowiki>{{{1|}}}</nowiki></tt>, sample B) with present empty default is often the desired way to handle situations where a parameter exists, but is comprised only of empty space.
  
 +
<!--T:15-->
 
One way to test whether a parameter was specified in a template call is thus:
 
One way to test whether a parameter was specified in a template call is thus:
 +
 +
<!--T:16-->
 
<tt><nowiki>{{#ifeq:{{{v|}}}|{{{v|-}}}| v was specified (and may be empty) | v was not specified }}</nowiki></tt>
 
<tt><nowiki>{{#ifeq:{{{v|}}}|{{{v|-}}}| v was specified (and may be empty) | v was not specified }}</nowiki></tt>
  
 +
<!--T:17-->
 
In rare cases, a template behaves differently when a parameter is unspecified compared to when it is specified but empty. When this template is used by a wrapper template (which uses the same set of named parameters), one way to ensure undefined parameters remain undefined is as follows (the technique also works with numbered parameters):
 
In rare cases, a template behaves differently when a parameter is unspecified compared to when it is specified but empty. When this template is used by a wrapper template (which uses the same set of named parameters), one way to ensure undefined parameters remain undefined is as follows (the technique also works with numbered parameters):
 +
</translate>
  
 
<pre>
 
<pre>
43行目: 74行目:
 
</pre>
 
</pre>
  
 +
<translate>
 +
<!--T:18-->
 
Note that the <tt>wrapped_template</tt> receives a specified but empty <tt>normal_parameter</tt> in two cases: when <tt>normal_parameter</tt> is specified and empty, and when <tt>normal_parameter</tt> is unspecified (because the default empty value is then specified by the wrapper template).
 
Note that the <tt>wrapped_template</tt> receives a specified but empty <tt>normal_parameter</tt> in two cases: when <tt>normal_parameter</tt> is specified and empty, and when <tt>normal_parameter</tt> is unspecified (because the default empty value is then specified by the wrapper template).
  
By contrast, the <tt>wrapped_template</tt> receives a specified <tt>sensitive_parameter</tt> ''only'' when it is indeed specified, because the <tt>#if</tt> changes the <tt>wrapped_template</tt> parameter name to « <tt>sensitive_parameterNULL</tt> » when <tt>sensitive_parameter</tt> is undefined. Note also that <tt>wrapped_template</tt> receives a specified and empty <tt>sensitive_parameter</tt> ''only'' when the wrapper template receives a specified and empty <tt>sensitive_parameter</tt>. Of course, one must make sure the suffixed <tt>wrapped_template</tt> parameter name is meaningless to the <tt>wrapped_template</tt> for this to work properly.
+
<!--T:19-->
 +
By contrast, the <tt>wrapped_template</tt> receives a specified <tt>sensitive_parameter</tt> ''only'' when it is indeed specified, because the <tt>#if</tt> changes the <tt>wrapped_template</tt> parameter name to « <tt>sensitive_parameterNULL</tt> » when <tt>sensitive_parameter</tt> is undefined.  
  
==See also==
+
<!--T:20-->
*[[Help:Extension:ParserFunctions]]
+
Note also that <tt>wrapped_template</tt> receives a specified and empty <tt>sensitive_parameter</tt> ''only'' when the wrapper template receives a specified and empty <tt>sensitive_parameter</tt>. Of course, one must make sure the suffixed <tt>wrapped_template</tt> parameter name is meaningless to the <tt>wrapped_template</tt> for this to work properly.
*[[Help:Template]]
 
*[[Extension:Loops#.23forargs_.28Experimental.29|Extension:Loops#forargs]]
 
  
{{Languages}}
+
==See also== <!--T:21-->
 +
</translate>
 +
* {{ll|Help:Extension:ParserFunctions}}
 +
* {{ll|Help:Templates}}
 +
<translate>
 +
<!--T:22-->
 +
*[[<tvar|ext>Special:MyLanguage/Extension:Loops#.23forargs_.28Experimental.29</>|Extension:Loops#forargs]]
 +
</translate>
  
[[Category:Extension help|Parser functions in templates]]
+
[[Category:Extension help{{translation}}|Parser functions in templates]]

2016年7月24日 (日) 00:25時点における最新版

PD Note: When you edit this page, you agree to release your contribution under the CC0. See Public Domain Help Pages for more info.
PD
</div>

When applying ParserFunctions to template parameters, a pipe symbol ("|") may be used to provide the empty string as default value, in order to indicate how to handle things if a parameter is not passed in. In the absence of this, an undefined parameter will not expand (i.e. will expand to {{{1}}}), which is not an empty string, and thus true.

  • {{{1}}}
Sample A
{{#if: {{{1}}} | Parameter 1 is not defined, or is defined and non-null/non-empty. | Parameter 1 is null. It contains only empty string(s) or breaking space(s) etc.}}
Result
Parameter 1 is not defined, or is defined and non-null/non-empty.
  • {{{1|}}}
Sample B
{{#if: {{{1|}}} | Parameter 1 is defined and non-null/non-empty. | Parameter 1 is not defined, or is defined but null. It contains only empty string(s) or breaking space(s) etc.}}
Result
Parameter 1 is not defined, or is defined but null. It contains only empty string(s) or breaking space(s) etc.
Comparison
Example of usage in a template
unnamed first parameter (1=)
named parameter (param=)
Condition of parameter Sample A:
{{#if: {{{1}}}|True|False}}
{{#if: {{{param}}}|True|False}}
Sample B:
{{#if: {{{1|}}}|True|False}}
{{#if: {{{param|}}}|True|False}}

{{template}}
not defined:
parameter not used,
important for named parameters
True False

{{template|}} .. {{template|1=}} .. {{template|param=}}
defined, but null/empty False False

{{template|value}} .. {{template|1=value}} .. {{template|param=value}}
defined
and
NOT null neither empty
True True

The second usage ({{{1|}}}, sample B) with present empty default is often the desired way to handle situations where a parameter exists, but is comprised only of empty space.

One way to test whether a parameter was specified in a template call is thus:

{{#ifeq:{{{v|}}}|{{{v|-}}}| v was specified (and may be empty) | v was not specified }}

In rare cases, a template behaves differently when a parameter is unspecified compared to when it is specified but empty. When this template is used by a wrapper template (which uses the same set of named parameters), one way to ensure undefined parameters remain undefined is as follows (the technique also works with numbered parameters):

<!-- wrapper template code -->
{{wrapped_template|
normal_parameter={{{normal_parameter|}}}|
sensitive_parameter{{#if:{{{sensitive_parameter|}}}||NULL}}={{{sensitive_parameter}}}|
...
}}

Note that the wrapped_template receives a specified but empty normal_parameter in two cases: when normal_parameter is specified and empty, and when normal_parameter is unspecified (because the default empty value is then specified by the wrapper template).

By contrast, the wrapped_template receives a specified sensitive_parameter only when it is indeed specified, because the #if changes the wrapped_template parameter name to « sensitive_parameterNULL » when sensitive_parameter is undefined.

Note also that wrapped_template receives a specified and empty sensitive_parameter only when the wrapper template receives a specified and empty sensitive_parameter. Of course, one must make sure the suffixed wrapped_template parameter name is meaningless to the wrapped_template for this to work properly.

See also[編集]

案内メニュー