Template:IfEmpty
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.