Template:Test-if/doc

From Angelina Jordan Wiki
Revision as of 17:02, 3 August 2022 by Dcljr (talk | contribs) (doc created)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
This is a subpage documenting Template:Test-if. To change the template, edit that page; to change the template's documentation (seen below), edit this one.

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 outside of this very documentation page.

Usage

Usage for this template is not relevant, but the form of an "if" parser function call is:

  • {{#if:test value or input|output if true|output if 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".

Examples

Parentheses are used in the following examples to clarify how leading and trailing whitespace is not preserved in the output (but "internal" whitespace is).

Hardcoded examples

Missing, empty, and whitespace-only values

  • ({{#if:}}) ⇒ ()
  • ({{#if:|}}) ⇒ ()
  • ({{#if:||}}) ⇒ ()
  • ({{#if: | | }}) ⇒ ()

False inputs and whitespace in the output

  • ({{#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| }}) ⇒ ()

True inputs and whitespace in the output

  • ({{#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||B}}) ⇒ ()
  • ({{#if:T| |B}}) ⇒ ()

Using undefined parameters

  • ({{#if:{{{9}}}|A|B}}) ⇒ (A)
  • ({{#if:{{{ten}}}|A|B}}) ⇒ (A)
  • ({{#if:{{{9|}}}|A|B}}) ⇒ (B)
  • ({{#if:{{{ten|}}}|A|B}}) ⇒ (B)

The first two examples in this last batch 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}}}. Instead, in such a case the value of {{{parameter}}} is the literal string "{{{parameter}}}". Thus, in the first example, the test value is the string "{{{9}}}", which evaluates to true. The construct in the third and fourth examples, using the "pipe" character to specify empty default values, is generally what was intended.

Whitespace in template calls

As seen in the examples above, leading and trailing whitespace, as well as whitespace-only values, are "lost" when using the {{#if:}} parser function. But the mere act of calling a template can also cause whitespace to be lost, as illustrated in the documentation of {{test-params}}.