Template:If

 The documentation for this template is transcluded from Template:If/doc. Please edit that page if you want to change something in the documentation. Do not edit this page unless you know what you're doing.
Usage
{{if|test=test string|then=code if exists|else=code if not}}
(other cases, in which some parameters are missing, are shown below)
Purpose
Provides simple if-then-else capability in templates.
Examples
Code Result
{{if|test=1|then=a|else=b}} a
{{if|test=|then=a|else=b}} b
{{if|test= |then=a|else=b}} b
{{if|test=1|then=a}} a
{{if|test=|then=a}}
{{if|test=}}
{{if|test=1|else=b}}
{{if|test=|else=b}} b
{{if|test=1}}
{{if|test=}}
{{if|then=a|else=b}} b
{{if|then=a}} a
{{if|else=b}} b
{{if}}
Technical description
In what follows, a "non-empty" string is one containing at least one non-whitespace character; an "empty" string contains no non-whitespace characters (thus, either no characters at all or only whitespace characters). Note in particular that mathematical or "logical" expressions, such as "0" or "FALSE" will evaluate as "true", since they are non-empty strings. See also WikimediaMeta:Template talk:Ifold.
• If the value of test is non-empty ("true"), then:
• if the value of then is defined, that value is produced,
• otherwise the empty string.
• If the value of test is empty ("false"), then:
• if the value of else is defined, that value is produced,
• otherwise the empty string.
• If the value of test is undefined, then:
• if the value of else is defined, that value is produced;
• if the value of else is also undefined, then:
• if the value of then is defined, that value is produced,
• otherwise the empty string.
Note that the rules for the case in which the value of test is undefined are somewhat arbitrary, and just a by-product of the code. Note in particular that in this case it makes a difference whether the value of else is empty or undefined; see the examples above.
Warning to editors
This template is used in other templates that are widely used in articles on this wiki. In addition, it relies on advanced template techniques. Don't edit it unless you really know what you're doing. If you just want to change something in this documentation, edit Template:If/doc, instead.