Help:Parser functions in templates
<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 a default value, which is used when a parameter is not defined.</translate> <translate> Used in an #if parser function, the unexpanded text from the undefined parameter will evaluate as true, which may be an unexpected result.</translate>
<translate> Parameter</translate> | {{{1}}}, {{{param}}} | {{{1|}}}, {{{param|}}} | {{#if:<<translate> parameter</translate>>|True|False}} | ||
---|---|---|---|---|---|
<translate> Description</translate> | <translate> Example, unnamed and named</translate> | {{{1}}}, {{{param}}} | {{{1|}}}, {{{param|}}} | ||
<translate> Undefined. More appropriate for use in named parameters.</translate> | {{template}}
|
{{{1}}} | True | False | |
<translate> Defined, but empty or null.</translate> | {{template|}} , {{template|1=}} , {{template|param=}}
|
False | False | ||
<translate> Defined, non-empty, and non-null.</translate> | {{template|value}} , {{template|1=value}} , {{template|param=value}}
|
value | value | True | True |
{{{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
-
{{#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.}}
</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
-
{{#if: | 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.}}
</translate>
<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.
Testing and passing undefined parameters
To distinguish a possibly empty parameter from an unspecified, one, compare it to itself using <tvar|ifeq>{{#ifeq:}}
</> and different defaults.</translate>
<translate>
What the defaults are does not matter as long as they are different, so they are typically chosen to be short.</translate>
<translate>
The following all work equivalently:
</translate>
{{#ifeq:{{{v|+}}}|{{{v|-}}}| <translate> v was defined (and may be empty)</translate> | <translate> v was not defined</translate> }}
{{#ifeq:{{{v|}}}|{{{v|-}}}| <translate> v was defined (and may be empty)</translate> | <translate> v was not defined</translate> }}
{{#ifeq:{{{v|}}}|{{{v}}}| <translate> v was defined (and may be empty)</translate> | <translate> v was not defined</translate> }}
<translate> 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>
{{wrapped_template| normal_parameter={{{normal_parameter|}}}| sensitive_parameter{{#if:{{{sensitive_parameter|}}}||NULL}}={{{sensitive_parameter}}} }}
<translate>
wrapped_template
receives a defined normal_parameter in all cases. When normal_parameter is defined but empty and when it is undefined, wrapped_template
receives an empty normal_parameter.
By contrast, the wrapped_template
receives a defined sensitive_parameter only when it is indeed defined; when sensitive_parameter is undefined, the #if
changes the parameter name to sensitive_parameterNULL.</translate>
<translate>
The suffixed parameter name must be 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>
[[Category:Extension help{{#translation:}}|Parser functions in templates]]