Help:Parser functions in templates

From The SBN Wiki
Jump to navigation Jump to search

<languages /> Template:TNT <translate> 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 or is not passed in. </translate>

  • {{{1}}}

<translate>

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.

</translate>

  • {{{1|}}}

<translate>

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

</translate>

<translate>

Example of usage in a template
unnamed first parameter (1=)
named parameter (param=)</translate>

<translate>

Condition of parameter</translate>

<translate>

Sample A:</translate>
{{#if: {{{1}}}| true| false}}
{{#if: {{{param}}}| true| false}}

<translate>

Sample B:</translate>
{{#if: {{{1|}}}| true| false}}
{{#if: {{{param|}}}| true| false}}


{{template}}
<translate>

not defined:
parameter not used,
important for named parameters</translate>||style="background-color: #AAFFAA;" align="center"| TRUE||style="background-color: #FFAAAA;" align="center"| FALSE


{{template|}} .. {{template|1=}} .. {{template|param=}}
<translate>

defined, but null/empty</translate>||style="background-color: #FFAAAA;" align="center"| FALSE||style="background-color: #FFAAAA;" align="center"| FALSE


{{template|value}} .. {{template|1=value}} .. {{template|param=value}}
<translate>

defined
and
NOT null neither empty</translate>||style="background-color: #AAFFAA;" align="center"| TRUE||style="background-color: #AAFFAA;" align="center"| TRUE

<translate> 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): </translate>

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

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

</translate>

<translate>

  • [[<tvar|ext>Special:MyLanguage/Extension:Loops#.23forargs_.28Experimental.29</>|Extension:Loops#forargs]]

</translate>