Template:Test-params

From Angelina Jordan Wiki
Revision as of 07:38, 3 August 2022 by Dcljr (talk | contribs) (…)

1 absent; 2 absent

Documentation

This template exists only to collect examples that illustrate certain vagaries of MediaWiki templates — in particular, cases where whitespace is or is not preserved, and how that affects the perceived definedness and truth value of the parameters passed to the template.

This is obviously only for the benefit of template editors (and maybe some template users) on this wiki, so the template should generally not be used outside of this very documentation page (and possibly on talk pages, to explain unexpected template behavior).

Usage

  • {{test-params}}
  • {{test-params|…}}
  • {{test-params|…|…}}

The only parameters this template checks for are the first two positional ones and their named equivalents, 1 and 2. Their behavior in the examples where they are named (e.g., using |1=) is the same as for any other named parameter.

Examples

In all examples, "fallback value" is the value of "{{{1|{{{2|default}}}}}}".

No parameters

  • {{test-params}} ⇒ 1 absent; 2 absent

Positional only: empty and whitespace

  • {{test-params|}} ⇒ 1 present; 2 absent
  • {{test-params||}} ⇒ 1 present; 2 present
  • {{test-params|| }} ⇒ 1 present; 2 present
  • {{test-params| }} ⇒ 1 present; 2 absent
  • {{test-params| |}} ⇒ 1 present; 2 present
  • {{test-params| | }} ⇒ 1 present; 2 present

Named only: empty and whitespace values

  • {{test-params|1=}} ⇒ 1 present; 2 absent
  • {{test-params|1= }} ⇒ 1 present; 2 absent
  • {{test-params|1=|2=}} ⇒ 1 present; 2 present
  • {{test-params|1=|2= }} ⇒ 1 present; 2 present
  • {{test-params|1= |2=}} ⇒ 1 present; 2 present
  • {{test-params|1= |2= }} ⇒ 1 present; 2 present
  • {{test-params|2=}} ⇒ 1 absent; 2 present
  • {{test-params|2= }} ⇒ 1 absent; 2 present

Named but unrecognized parameter (not checked for) and empty positional

  • {{test-params|ignored=}} ⇒ 1 absent; 2 absent
  • {{test-params|ignored=|}} ⇒ 1 present; 2 absent
  • {{test-params||ignored=}} ⇒ 1 present; 2 absent
  • {{test-params||ignored=|}} ⇒ 1 present; 2 present

Mix of named and positional: empty and whitespace values

  • {{test-params|1=|}} ⇒ 1 present; 2 absent   [bad call: 1 defined twice]
  • {{test-params|1=| }} ⇒ 1 present; 2 absent   [bad call: 1 defined twice]
  • {{test-params|1= |}} ⇒ 1 present; 2 absent   [bad call: 1 defined twice]
  • {{test-params|1= | }} ⇒ 1 present; 2 absent   [bad call: 1 defined twice]
  • {{test-params||2=}} ⇒ 1 present; 2 present
  • {{test-params||2= }} ⇒ 1 present; 2 present
  • {{test-params| |2=}} ⇒ 1 present; 2 present
  • {{test-params| |2= }} ⇒ 1 present; 2 present
  • {{test-params|2=|}} ⇒ 1 present; 2 present
  • {{test-params|2= | }} ⇒ 1 present; 2 present

Positional only: values with optional leading/trailing whitespace

  • {{test-params|foo}} ⇒ 1 present; 2 absent
  • {{test-params|foo|bar}} ⇒ 1 present; 2 present
  • {{test-params| foo | bar }} ⇒ 1 present; 2 present
  • {{test-params| | bar }} ⇒ 1 present; 2 present

Named only: values with optional leading/trailing whitespace

  • {{test-params|1=foo}} ⇒ 1 present; 2 absent
  • {{test-params|1=foo|2=bar}} ⇒ 1 present; 2 present
  • {{test-params|1= foo |2= bar }} ⇒ 1 present; 2 present
  • {{test-params|1= |2= bar }} ⇒ 1 present; 2 present
  • {{test-params|2= bar }} ⇒ 1 absent; 2 present

Positional only: non-whitespace and empty/whitespace-only values

  • {{test-params|foo|}} ⇒ 1 present; 2 present
  • {{test-params|foo| }} ⇒ 1 present; 2 present
  • {{test-params||bar}} ⇒ 1 present; 2 present
  • {{test-params| |bar}} ⇒ 1 present; 2 present

Named only: non-whitespace and empty/whitespace-only values

  • {{test-params|1=foo|2=}} ⇒ 1 present; 2 present
  • {{test-params|1=foo|2= }} ⇒ 1 present; 2 present
  • {{test-params|1=|2=bar}} ⇒ 1 present; 2 present
  • {{test-params|1= |2=bar}} ⇒ 1 present; 2 present

Mix of named with non-whitespace values and empty/whitespace positional

  • {{test-params|1=foo|}} ⇒ 1 present; 2 absent   [bad call: 1 defined twice]
  • {{test-params|1=foo| }} ⇒ 1 present; 2 absent   [bad call: 1 defined twice]
  • {{test-params||2=bar}} ⇒ 1 present; 2 present
  • {{test-params| |2=bar}} ⇒ 1 present; 2 present

Mix of positional with non-whitespace values and empty/whitespace-only named

  • {{test-params|foo|2=}} ⇒ 1 present; 2 present
  • {{test-params|foo|2= }} ⇒ 1 present; 2 present
  • {{test-params|foo|2=bar}} ⇒ 1 present; 2 present
  • {{test-params|1=|bar}} ⇒ 1 present; 2 absent   [bad call: 1 defined twice]
  • {{test-params|1= |bar}} ⇒ 1 present; 2 absent   [bad call: 1 defined twice]
  • {{test-params|1=foo|bar}} ⇒ 1 present; 2 absent   [bad call: 1 defined twice]

TL;DR

Here's a single, compact example, showing most of the relevant issues. The following template call defines several parameters, some apparently containing whitespace and others not:

  • {{test-params-call|| |three| four |five=|six= |seven=7| eight = 8 }}

So, in particular, the first four positional parameters are all defined, with 1 being empty, 2 containing only whitespace, and 3 and 4 containing non-whitepace but with 4 also containing leading and trailing whitespace. The rest are named parameters, with the value of five being empty, six being only whitespace, seven being only non-whitespace, and eight having leading and trailing whitespace (both around the parameter name and its value).

However, the following list shows the value of each of these parameters as actually seen by (or "inside of") the called template:

  • ({{{1}}}) ⇒ ()
  • ({{{2}}}) ⇒ ( )
  • ({{{3}}}) ⇒ (three)
  • ({{{4}}}) ⇒ ( four )
  • ({{{five}}}) ⇒ ()
  • ({{{six}}}) ⇒ ()
  • ({{{seven}}}) ⇒ (7)
  • ({{{eight}}}) ⇒ (8)

Notice how the values of 1, 2, 3, 4, five, and seven are as expected, but the values of six and eight are missing the whitespace that was seen in the template call. This is because parameters defined "positionally" (so-called "unnamed" or "anonymous" parameters) retain the whitespace seen in the template call, whereas the values of named parameters lose any leading or trailing whitespace seen in the template call.

As for their truth values, 1, 2, five, and six will be seen as false, and 3, 4, seven, and eight true.

For good measure, here's how the template sees the values of parameters that are not defined at all:

  • ({{{9}}}) ⇒ ({{{9}}})
  • ({{{ten}}}) ⇒ ({{{ten}}})
  • ({{{9|}}}) ⇒ ()
  • ({{{ten|}}}) ⇒ ()

The first 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}}}. Instead, in such a case the value of {{{parameter}}} is the literal string "{{{parameter}}}". Thus, in the first two examples, the test values are, respectively, the literal strings "{{{9}}}" and "{{{ten}}}", which, of course, would evaluate to true. The construct seen in the third and fourth examples, using the "pipe" character to specify an empty default value, is usually what was intended.

See also

  • {{test-if}} — a similar template documenting how whitespace is handled when using the {{#if:}} parser function