Help:Truth value

From Angelina Jordan Wiki

A template parameter is considered to be "true" if its value contains at least one non-whitespace character.

For example, the template call {{TV 2|link=y}} sets the parameter named link equal to the true value y to indicate that the template should provide a link (see the template's documentation for an explanation in the context of that particular template). Note that the fact that the letter "y" happens to connote "yes" is irrelevant: even values such as 0 and FALSE are true, because they are strings containing non-whitespace. (But, of course, saying {{TV 2|link=FALSE}} when you want a link is a bit silly.)

By contrast, none of the calls {{TV 2|link=}}, {{TV 2|link= }}, and {{TV 2}} set link to a true value.

The situation is the same for so-called "unnamed" or "positional" parameters. The template calls {{all-of|1}}, {{all-of|none}}, and {{all-of|1=two}} are passing true values in the first positional parameter (the values 1, none, and two, respectively), whereas the calls {{all-of}}, {{all-of|}}, {{all-of| }}, and {{all-of|1= }} are not. (As seen in these examples, the name 1 can be used to explicitly refer to the first positional parameter. The last example is setting the first positional parameter equal to whitespace. Well… okay, technically it's setting it equal to nothing. See Template:Test-params for why.)

Parser functions

There are two MediaWiki parser functions that determine the truth value of their input, but they do it in very different ways:

  • {{#if:}} selects between two alternatives based on whether a given string contains at least one non-whitespace character, a true value; if the string contains only whitespace or no characters at all, it is considered false
  • {{#ifexpr}} selects between two alternatives based on whether a numerical and/or logical expression evaluates to 1 (true) or 0 (false); for the purpose of interpreting input, the Boolean functions that can be used in the expression (and, or, not, and so forth) treat 0 as false and any other value as true.

So far, {{#ifexpr:}} has not seen any use on this wiki, so the first interpretation of truth is always the relevant one here. For further information about how {{#if:}} works, see the documentation of Template:Test-if and the relevant help at MediaWiki.org. For more about template parameters, see Template:Test-params.

Testing for definedness rather than truth

In some situations, you want a template to do something when a certain parameter was present (a.k.a. "defined") in the template call, regardless of whether that parameter was given a true or false value. In that case, you can do something like this:

{{#ifeq:{{{1|+}}}|{{{1|-}}}|…parameter 1 was defined…|…parameter 1 was not defined…}}