Template:Test-if/doc
Documentation
This template exists only to collect examples that illustrate how the {{#if:}} parser function works.
This is obviously only for the benefit of template editors on this wiki, so the template should generally not be used, well, anywhere.
Usage
Usage for this template is not relevant, but the form of an "if" parser function call is one of the following:
{{#if:test value or input|output if true}}{{#if:test value or input|output if true|output if false}}
In the first form, nothing is output if the test value is false.
Inputs evaluating to "true" are those containing at least one non-whitespace character. As seen in the examples below, missing/empty inputs and inputs containing only whitespace are considered "false".
Basic Examples
{{#if:FOO|any non-whitespace input is true|not false}}⇒ any non-whitespace input is true{{#if:|blah|missing input is false}}⇒ missing input is false{{#if: |blah|whitespace-only input is false}}⇒ whitespace-only input is false
Whitespace
Parentheses are used in the following examples to clarify how leading and trailing whitespace is not preserved in the output (but "internal" whitespace is).
Missing, empty, and whitespace-only values
({{#if:}})⇒ ()({{#if:|}})⇒ ()({{#if:||}})⇒ ()({{#if: | | }})⇒ ()
In all of these examples, the test value is interpreted as "false", so the "output if false" is used — which is missing in the first two examples, empty in the third, and whitespace-only in the fourth (but this whitespace is "lost", as illustrated in more detail below).
False inputs and missing/empty/whitespace outputs
({{#if:|A|B}})⇒ (B)({{#if: |A|B}})⇒ (B)({{#if: | A | B }})⇒ (B)({{#if: | AA | BB }})⇒ (BB)({{#if: | A A | B B }})⇒ (B B)({{#if:|A}})⇒ ()({{#if:|A|}})⇒ ()({{#if:|A| }})⇒ ()({{#if:|A| }})⇒ ()
True inputs and missing/empty/whitespace outputs
({{#if:T|A|B}})⇒ (A)({{#if: T | A | B }})⇒ (A)({{#if: T | AA | BB }})⇒ (AA)({{#if: T | A A | B B }})⇒ (A A)({{#if:T|A}})⇒ (A)({{#if:T|A|}})⇒ (A)({{#if:T||B}})⇒ ()({{#if:T| |B}})⇒ ()({{#if:T| |B}})⇒ ()
As seen in the examples above, leading and trailing whitespace, as well as whitespace-only values, are "lost" when using the {{#if:}} parser function. The workaround is to use <nowiki> in one of the following ways:
({{#if:T|<nowiki /> A }})⇒ ( A)({{#if:T| A <nowiki />}})⇒ (A )({{#if:T|<nowiki /> A <nowiki />}})⇒ ( A )({{#if:T|<nowiki> </nowiki>A }})⇒ ( A)({{#if:T| A<nowiki> </nowiki>}})⇒ (A )({{#if:T|<nowiki> A </nowiki>}})⇒ ( A )({{#if:T|<nowiki> </nowiki>}})⇒ ( )({{#if:T|<nowiki> </nowiki>}})⇒ ( )
Note how either an empty element (<nowiki />) or a non-empty one (<nowiki>…</nowiki>) can be used.
Also, note that the mere act of calling a template can also cause whitespace to be lost (in particular, when named parameters are used), as illustrated in the documentation of {{test-params}}. For example:
{{wl|1= A B }}⇒[[A B]]{{wl| A B }}⇒[[ A B ]]
Using undefined parameters
Undefined parameters have no value, and so are equivalent to missing or empty values:
({{#if:{{{9|}}}|A|B}})⇒ (B)({{#if:{{{param|}}}|A|B}})⇒ (B)
But:
({{#if:{{{9}}}|A|B}})⇒ (A)({{#if:{{{param}}}|A|B}})⇒ (A)
The last two examples here show a common mistake in template coding: assuming that a parameter that is not defined in the template call will result in a null or empty value when referred to like this: {{{parameter-name}}}. Instead, in such a case the value of {{{parameter-name}}} is the literal string "{{{parameter-name}}}", which will always be true. To fix the problem, simply use the "pipe" character (|) to specify an empty default value: {{{parameter-name|}}} will evaluate to false if that parameter was not defined in the template call.
Special characters
A leading semicolon, colon, or asterisk in the output will be interpreted as wiki markup (even if the {{#if:}} call doesn't appear at the beginning of a line), creating a definition-list term (bold), definition-list definition (indentation), unordered-list item (bullet), respectively:
{{#if:T|;A;}}⇒
- A;
{{#if:T|:A:}}⇒
- A:
{{#if:T|*A*}}⇒- A*
These can be fixed using <nowiki> in one of the following ways:
{{#if:T|<nowiki />;A;}}⇒ ;A;{{#if:T|<nowiki>;</nowiki>A;}}⇒ ;A;{{#if:T|<nowiki>;A;</nowiki>}}⇒ ;A;
Unlike in regular template calls, an equals symbol is not interpreted as separating a parameter name from its value:
{{#if:T|A=B}}⇒ A=B
Compare:
{{code|A=B}}⇒code{{code|1=A=B}}⇒A=B{{code}}⇒code
(The {{code}} template defaults to using the word "code" if no input is given to it.)