Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Template:If empty/doc: Difference between revisions

Template page
m 1 revision imported
wikipeida>HTGS
Clean up examples: Some were not displaying pipes properly because of {{tlc}}, other examples were unnecessary, others needed some explanatory text
Line 2: Line 2:
<!----PLEASE ADD TEMPLATE CATEGORIES WHERE INDICATED AT THE BOTTOM OF THIS PAGE---->
<!----PLEASE ADD TEMPLATE CATEGORIES WHERE INDICATED AT THE BOTTOM OF THIS PAGE---->
{{Cascade-protected template}}
{{Cascade-protected template}}
{{High-use|all-pages = yes}}
{{used in system}}
{{Lua|Module:If empty}}
{{Lua|Module:If empty}}


Line 9: Line 9:
Typical usage is like this:
Typical usage is like this:
: <code><nowiki>{{If empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}</nowiki></code>
: <code><nowiki>{{If empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}</nowiki></code>
: <nowiki>("Return {{{logo|}}} if it has a value; else {{{image|}}} if it has a value; else {{{picture|}}} if it has a value; else return "default.svg".")</nowiki>
: <nowiki>Return {{{logo|}}} if it has a value; else {{{image|}}} if it has a value; else {{{picture|}}} if it has a value; else return "default.svg".</nowiki>
This returns the first of the parameters ''logo'', ''image'' and ''picture'' that is defined and non-empty, otherwise "default.svg".
This returns the first of the parameters ''logo'', ''image'' and ''picture'' that is defined and non-empty, otherwise "default.svg".


Line 17: Line 17:
The usual workaround for a single parameter is:
The usual workaround for a single parameter is:
: <code><nowiki>{{#if:{{{logo|}}} |{{{logo}}} |default.svg}}</nowiki></code>
: <code><nowiki>{{#if:{{{logo|}}} |{{{logo}}} |default.svg}}</nowiki></code>
: <nowiki>("If {{{logo}}} has a value, return it, else return "default.svg".")</nowiki>
: <nowiki>If {{{logo}}} has a value, return it, else return "default.svg".</nowiki>


But this becomes complex when several parameters are to be checked:
But this becomes complex when several parameters are to be checked:
: <code><nowiki>{{#if:{{{logo|}}} |{{{logo}}} |{{#if:{{{image|}}} |{{{image}}} |{{#if:{{{picture|}}} |{{{picture}}} |default.svg}} }} }}</nowiki></code>
: <code><nowiki>{{#if:{{{logo|}}} |{{{logo}}} |{{#if:{{{image|}}} |{{{image}}} |{{#if:{{{picture|}}} |{{{picture}}} |default.svg}} }} }}</nowiki></code>
: <nowiki>("If {{{logo}}} has a value, return it; else if {{{image}}} has a value, return that; else if {{{picture}}} has a value, return that; else return "default.svg".")</nowiki>
: <nowiki>If {{{logo}}} has a value, return it; else if {{{image}}} has a value, return that; else if {{{picture}}} has a value, return that; else return "default.svg".</nowiki>
In these cases, {{tlf|if empty}} produces the simpler syntax (as above):
In these cases, {{tlf|if empty}} produces the simpler syntax (as above):
: <code><nowiki>{{if empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}</nowiki></code>
: <code><nowiki>{{if empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}</nowiki></code>
Line 35: Line 35:
  | {{tlc|if empty}} | {{if empty}} | Returns an empty string.
  | {{tlc|if empty}} | {{if empty}} | Returns an empty string.
  | {{tlc|if empty|one}} | {{if empty|one}} | Returns the first parameter that is defined and not empty.
  | {{tlc|if empty|one}} | {{if empty|one}} | Returns the first parameter that is defined and not empty.
| {{tlc|if empty|one|two}} | {{if empty|one|two}} |
  | {{tlc|if empty|one|two|three|four}} | {{if empty|one|two|three|four}} |  
  | {{tlc|if empty|one|two|three|four}} | {{if empty|one|two|three|four}} |  
  | {{tlc|if empty||two}} | {{if empty||two}} | The first parameter is empty/undefined, so is passed over.
  | <code><nowiki>{{if empty||two}}</nowiki></code> | {{if empty||two}} | The first parameter is empty/undefined, so is passed over.
  | {{tlc|if empty||two|three|four}} | {{if empty||two|three|four}} |
  | <code><nowiki>{{if empty||two||four}}</nowiki></code> | {{if empty||two||four}} |  
| {{tlc|if empty||two||four}} | {{if empty||two||four}} |  
  | <code><nowiki>{{if empty||||||||||ten}}</nowiki></code> | {{if empty||||||||||ten}} | Unlike in previous versions, this template is no longer limited by 9 parameters.
  | <code><nowiki>{{if empty||||||||||ten}}</nowiki></code> | {{if empty||||||||||ten}} | Unlike in previous versions, this template is no longer limited by 9 parameters.
  | {{tlc|if empty{{!}}}} | {{if empty|}} | The only parameter is empty or undefined, so returns an empty string.
  | {{tlc|if empty{{!}}}} | {{if empty|}} | The only parameter is empty or undefined, so the template returns an empty string.
  | {{tlc|if empty|{{!}}{{!}}{{!}}{{!}}}} | {{if empty||||}} | Returns an empty string.
  | {{tlc|if empty|{{!}}{{!}}{{!}}{{!}}}} | {{if empty||||}} | Returns an empty string.
  | <code><nowiki>{{if empty|{{{1|}}}|{{{2|}}}|three}}</nowiki></code> | {{if empty|{{{1|}}}|{{{2|}}}|three}} |  
  | <code><nowiki>{{if empty|{{{1|}}}|{{{2|}}}|three}}</nowiki></code> | {{if empty|{{{1|}}}|{{{2|}}}|three}} | Neither 1 nor 2 are defined, so "three" is returned.
  | <code><nowiki>{{if empty|{{{1}}}|{{{2}}}|three}}</nowiki></code> | {{if empty|{{{1}}}|{{{2}}}|three}} | No pipe-characters following the names of the first two parameters ("1" and "2"), so the first of these returned as text ("{{{1}}}").
  | <code><nowiki>{{if empty|{{{1}}}|{{{2}}}|three}}</nowiki></code> | {{if empty|{{{1}}}|{{{2}}}|three}} | No pipe-characters following the names of the first two parameters ("1" and "2"), so the first is returned as text ("{{{1}}}").
  | <code><nowiki>{{if empty|{{{logo|}}}|two}}</nowiki></code> | {{if empty|{{{logo|}}}|two}} |  
  | <code><nowiki>{{if empty|{{{logo|}}}|two}}</nowiki></code> | {{if empty|{{{logo|}}}|two}} |  
  | <code><nowiki>{{if empty|{{{logo}}}|two}}</nowiki></code> | {{if empty|{{{logo}}}|two}} | No pipe-character following the parameter name "logo", so the text "{{{logo}}}" returned.
  | <code><nowiki>{{if empty|{{{logo}}}|two}}</nowiki></code> | {{if empty|{{{logo}}}|two}} | No pipe-character following the parameter name "logo", so the text "{{{logo}}}" is returned.
  | <code><nowiki>{{if empty|p=q}}</nowiki></code> | {{if empty|p=q}} | The template identifies the parameters it receives as parameters 1 to 9, not using names such as "p", etc.
  | <code><nowiki>{{if empty|p=q}}</nowiki></code> | {{if empty|p=q}} | The template identifies the parameters it receives as parameters 1 to 9, not using names such as "p", etc.
}}
}}

Revision as of 02:17, 17 August 2024

Template:Cascade-protected template Template:Used in system

This template is used inside other templates. It takes any number of parameters and returns the first found to be defined and non-empty. If none is found, nothing is returned. By writing {{if empty|a|b|c}} instead of {{#if:a|a|{{#if:b|b|c}}}}, expression a and b do not have to be repeated and evaluated twice. The template provides a fallback order, similar to a try catch based "It is easier to ask for forgiveness than permission" (EAFP) programming style.

Typical usage is like this:

{{If empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}
— Return {{{logo|}}} if it has a value; else {{{image|}}} if it has a value; else {{{picture|}}} if it has a value; else return "default.svg".

This returns the first of the parameters logo, image and picture that is defined and non-empty, otherwise "default.svg".

Rationale

The MediaWiki parameter default function doesn't return the default value for empty parameters. That is, {{{logo|default.svg}}} does not return "default.svg" if the template was called like this: {{template|logo=}}.

The usual workaround for a single parameter is:

{{#if:{{{logo|}}} |{{{logo}}} |default.svg}}
— If {{{logo}}} has a value, return it, else return "default.svg".

But this becomes complex when several parameters are to be checked:

{{#if:{{{logo|}}} |{{{logo}}} |{{#if:{{{image|}}} |{{{image}}} |{{#if:{{{picture|}}} |{{{picture}}} |default.svg}} }} }}
— If {{{logo}}} has a value, return it; else if {{{image}}} has a value, return that; else if {{{picture}}} has a value, return that; else return "default.svg".

In these cases, {{if empty}} produces the simpler syntax (as above):

{{if empty |{{{logo|}}} |{{{image|}}} |{{{picture|}}} |default.svg}}

Piping

Parameters used with {{if empty}} must be piped – i.e. include the vertical bar (pipe) symbol ("|") as a trailing character – so that empty or undefined parameters aren't treated as text and returned incorrectly. Hence, for example, {{{logo|}}}, {{{image|}}} and {{{picture|}}} rather than Template:Param, Template:Param and Template:Param in the above examples.

Examples

Code Result
{{if empty}} Returns an empty string.
{{if empty|one}} one Returns the first parameter that is defined and not empty.
{{if empty|one|two|three|four}} one
{{if empty||two}} two The first parameter is empty/undefined, so is passed over.
{{if empty||two||four}} two
{{if empty||||||||||ten}} ten Unlike in previous versions, this template is no longer limited by 9 parameters.
{{if empty|}} The only parameter is empty or undefined, so the template returns an empty string.
{{if empty|||||}} Returns an empty string.
{{if empty|{{{1|}}}|{{{2|}}}|three}} three Neither 1 nor 2 are defined, so "three" is returned.
{{if empty|{{{1}}}|{{{2}}}|three}} {{{1}}} No pipe-characters following the names of the first two parameters ("1" and "2"), so the first is returned as text ("{{{1}}}").
{{if empty|{{{logo|}}}|two}} two
{{if empty|{{{logo}}}|two}} {{{logo}}} No pipe-character following the parameter name "logo", so the text "{{{logo}}}" is returned.
{{if empty|p=q}} The template identifies the parameters it receives as parameters 1 to 9, not using names such as "p", etc.

See also