Template:If empty: Difference between revisions

From IxWiki
Jump to navigation Jump to search
wp>Primefac
m Changed protection level for "Template:If empty": matching module permission ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))
 
m 1 revision imported
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{<includeonly>safesubst:</includeonly>#invoke:If empty|main}}<noinclude>{{Documentation}}</noinclude>
<includeonly>{{#invoke:If empty|main}}</includeonly><noinclude>{{/doc}}</noinclude>

Latest revision as of 01:42, 2 August 2024

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".