Template:IfEmpty: Difference between revisions
(Created page with "{{<includeonly>safesubst:</includeonly>#invoke:IfEmpty|main}}") |
No edit summary |
||
Line 1: | Line 1: | ||
{{<includeonly>safesubst:</includeonly>#invoke:IfEmpty|main}} | <noinclude>Accepts any number of parameters and returns the first value that's not empty. The more correct name for this template would be <code>FirstNonEmpty</code>, but most of the time you use it with two arguments, meaning that it either returns the first, or ''if that one is empty'' (hence <code>IfEmpty</code>), the second. | ||
You would typically use this while writing other templates, like so: | |||
<pre> | |||
{{IfEmpty | {{{firstParameter|}}} | {{{secondParameter|}}} }} | |||
</pre> | |||
Note that this is almost but not quite equivalent to: | |||
<pre> | |||
{{{firstParameter| {{{secondParameter|}}} }}} | |||
</pre> | |||
The difference is: | |||
In the second variant, if the user had explicitly provided a blank value for the first parameter, like <code><nowiki>{{MyTemplate | firstParameter = | secondParameter = x }}</nowiki></code> then it would '''not''' fall back to the value of the second parameter and instead return a blank. | |||
On the other hand, <code>IfEmpty</code> doesn't care whether the template parameter was omitted entirely, or provided with a blank value, so in both cases it will fall back to the second parameter and give you <code>x</code>. | |||
This is particularly useful when one template passes optional parameters onto another one, which eliminates the distinction between "not provided" and "blank value" since it passes blank values for all non-provided parameters onto the second template: | |||
<pre> | |||
{{MyOtherTemplate | |||
| firstParameter = {{{firstParameter|}}} | |||
| secondParameter = {{{secondParameter|}}} | |||
}} | |||
</pre> | |||
If you used a construct like <code><nowiki>{{{firstParameter | {{{secondParameter|}}} }}}</nowiki></code> in the definition of <code>MyOtherTemplate</code>, this would essentially be an error, since the first parameter will always have a value, even if it's blank, and so the fallback to the second parameter will never trigger. | |||
</noinclude>{{<includeonly>safesubst:</includeonly>#invoke:IfEmpty|main}} |
Latest revision as of 19:54, 23 July 2023
Accepts any number of parameters and returns the first value that's not empty. The more correct name for this template would be FirstNonEmpty
, but most of the time you use it with two arguments, meaning that it either returns the first, or if that one is empty (hence IfEmpty
), the second.
You would typically use this while writing other templates, like so:
{{IfEmpty | {{{firstParameter|}}} | {{{secondParameter|}}} }}
Note that this is almost but not quite equivalent to:
{{{firstParameter| {{{secondParameter|}}} }}}
The difference is:
In the second variant, if the user had explicitly provided a blank value for the first parameter, like {{MyTemplate | firstParameter = | secondParameter = x }}
then it would not fall back to the value of the second parameter and instead return a blank.
On the other hand, IfEmpty
doesn't care whether the template parameter was omitted entirely, or provided with a blank value, so in both cases it will fall back to the second parameter and give you x
.
This is particularly useful when one template passes optional parameters onto another one, which eliminates the distinction between "not provided" and "blank value" since it passes blank values for all non-provided parameters onto the second template:
{{MyOtherTemplate | firstParameter = {{{firstParameter|}}} | secondParameter = {{{secondParameter|}}} }}
If you used a construct like {{{firstParameter | {{{secondParameter|}}} }}}
in the definition of MyOtherTemplate
, this would essentially be an error, since the first parameter will always have a value, even if it's blank, and so the fallback to the second parameter will never trigger.