Template:If

(Difference between revisions)
Jump to: navigation, search
(Examples: clean up table a bit; use {t}; other minor)
(such a sensitive template, I'm moving the documentation to a "subpage" and protecting this one)
Line 1: Line 1:
 
{{{else{{{test|}}}|{{{test{{{test|}}}|{{{then|}}}}}}}}}<noinclude>
 
{{{else{{{test|}}}|{{{test{{{test|}}}|{{{then|}}}}}}}}}<noinclude>
==Description==
+
{{If/doc}}
: See also [[WikimediaMeta:Template:If]].
+
*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:
+
**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 that 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 below.
+
 
+
== Examples ==
+
Overview of all 12 cases:
+
{| class="wikitable"
+
! Code
+
! Result
+
|- valign="top"
+
| {{t|If|test&#61;1|then&#61;a|else&#61;b}}
+
| {{If|test=1|then=a|else=b}}
+
|- valign="top"
+
| {{t|If|test&#61;|then&#61;a|else&#61;b}}
+
| {{If|test=|then=a|else=b}}
+
|- valign="top"
+
| {{t|If|test&#61;1|then&#61;a}}
+
| {{If|test=1|then=a}}
+
|- valign="top"
+
| {{t|If|test&#61;|then=&#61;}}
+
| {{If|test=|then=a}}
+
|- valign="top"
+
| {{t|If|test&#61;1|else&#61;b}}
+
| {{If|test=1|else=b}}
+
|- valign="top"
+
| {{t|If|test&#61;|else&#61;b}}
+
| {{If|test=|else=b}}
+
|- valign="top"
+
| {{t|If|test&#61;1}}
+
| {{If|test=1}}
+
|- valign="top"
+
| {{t|If|test&#61;}}
+
| {{If|test=}}
+
|- valign="top"
+
| {{t|If|then&#61;a|else&#61;b}}
+
| {{If|then=a|else=b}}
+
|- valign="top"
+
| {{t|If|then&#61;a}}
+
| {{If|then=a}}
+
|- valign="top"
+
| {{t|If|else&#61;b}}
+
| {{If|else=b}}
+
|- valign="top"
+
| {{t|If}}
+
| {{If}}
+
|}
+
 
[[Category:Utility templates|If]]
 
[[Category:Utility templates|If]]
 +
[[Category:Templates|If]]
 
</noinclude>
 
</noinclude>

Revision as of 03:04, 23 June 2008


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.
Personal tools
Namespaces
Variants
Actions
wiki navigation
IronChariots.Org
Toolbox