Template:Test-params
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 undefined; 2 undefined; fallback value = "default"
Positional only: empty and whitespace
{{test-params|}}⇒ 1 defined and false (); 2 undefined; fallback value = ""{{test-params||}}⇒ 1 defined and false (); 2 defined and false (); fallback value = ""{{test-params|| }}⇒ 1 defined and false (); 2 defined and false ( ); fallback value = ""{{test-params| }}⇒ 1 defined and false ( ); 2 undefined; fallback value = " "{{test-params| |}}⇒ 1 defined and false ( ); 2 defined and false (); fallback value = " "{{test-params| | }}⇒ 1 defined and false ( ); 2 defined and false ( ); fallback value = " "
Named only: empty and whitespace values
{{test-params|1=}}⇒ 1 defined and false (); 2 undefined; fallback value = ""{{test-params|1= }}⇒ 1 defined and false (); 2 undefined; fallback value = ""{{test-params|1=|2=}}⇒ 1 defined and false (); 2 defined and false (); fallback value = ""{{test-params|1=|2= }}⇒ 1 defined and false (); 2 defined and false (); fallback value = ""{{test-params|1= |2=}}⇒ 1 defined and false (); 2 defined and false (); fallback value = ""{{test-params|1= |2= }}⇒ 1 defined and false (); 2 defined and false (); fallback value = ""{{test-params|2=}}⇒ 1 undefined; 2 defined and false (); fallback value = ""{{test-params|2= }}⇒ 1 undefined; 2 defined and false (); fallback value = ""
Named but unrecognized parameter (not checked for) and empty positional
{{test-params|ignored=}}⇒ 1 undefined; 2 undefined; fallback value = "default"{{test-params|ignored=|}}⇒ 1 defined and false (); 2 undefined; fallback value = ""{{test-params||ignored=}}⇒ 1 defined and false (); 2 undefined; fallback value = ""{{test-params||ignored=|}}⇒ 1 defined and false (); 2 defined and false (); fallback value = ""
Mix of named and positional: empty and whitespace values
{{test-params|1=|}}⇒ 1 defined and false (); 2 undefined; fallback value = "" [bad call: 1 defined twice]{{test-params|1=| }}⇒ 1 defined and false ( ); 2 undefined; fallback value = " " [bad call: 1 defined twice]{{test-params|1= |}}⇒ 1 defined and false (); 2 undefined; fallback value = "" [bad call: 1 defined twice]{{test-params|1= | }}⇒ 1 defined and false ( ); 2 undefined; fallback value = " " [bad call: 1 defined twice]{{test-params||2=}}⇒ 1 defined and false (); 2 defined and false (); fallback value = ""{{test-params||2= }}⇒ 1 defined and false (); 2 defined and false (); fallback value = ""{{test-params| |2=}}⇒ 1 defined and false ( ); 2 defined and false (); fallback value = " "{{test-params| |2= }}⇒ 1 defined and false ( ); 2 defined and false (); fallback value = " "{{test-params|2=|}}⇒ 1 defined and false (); 2 defined and false (); fallback value = ""{{test-params|2= | }}⇒ 1 defined and false ( ); 2 defined and false (); fallback value = " "
Positional only: values with optional leading/trailing whitespace
{{test-params|foo}}⇒ 1 defined and true (foo); 2 undefined; fallback value = "foo"{{test-params|foo|bar}}⇒ 1 defined and true (foo); 2 defined and true (bar); fallback value = "foo"{{test-params| foo | bar }}⇒ 1 defined and true ( foo ); 2 defined and true ( bar ); fallback value = " foo "{{test-params| | bar }}⇒ 1 defined and false ( ); 2 defined and true ( bar ); fallback value = " "
Named only: values with optional leading/trailing whitespace
{{test-params|1=foo}}⇒ 1 defined and true (foo); 2 undefined; fallback value = "foo"{{test-params|1=foo|2=bar}}⇒ 1 defined and true (foo); 2 defined and true (bar); fallback value = "foo"{{test-params|1= foo |2= bar }}⇒ 1 defined and true (foo); 2 defined and true (bar); fallback value = "foo"{{test-params|1= |2= bar }}⇒ 1 defined and false (); 2 defined and true (bar); fallback value = ""{{test-params|2= bar }}⇒ 1 undefined; 2 defined and true (bar); fallback value = "bar"
Positional only: non-whitespace and empty/whitespace-only values
{{test-params|foo|}}⇒ 1 defined and true (foo); 2 defined and false (); fallback value = "foo"{{test-params|foo| }}⇒ 1 defined and true (foo); 2 defined and false ( ); fallback value = "foo"{{test-params||bar}}⇒ 1 defined and false (); 2 defined and true (bar); fallback value = ""{{test-params| |bar}}⇒ 1 defined and false ( ); 2 defined and true (bar); fallback value = " "
Named only: non-whitespace and empty/whitespace-only values
{{test-params|1=foo|2=}}⇒ 1 defined and true (foo); 2 defined and false (); fallback value = "foo"{{test-params|1=foo|2= }}⇒ 1 defined and true (foo); 2 defined and false (); fallback value = "foo"{{test-params|1=|2=bar}}⇒ 1 defined and false (); 2 defined and true (bar); fallback value = ""{{test-params|1= |2=bar}}⇒ 1 defined and false (); 2 defined and true (bar); fallback value = ""
Mix of named with non-whitespace values and empty/whitespace positional
{{test-params|1=foo|}}⇒ 1 defined and false (); 2 undefined; fallback value = "" [bad call: 1 defined twice]{{test-params|1=foo| }}⇒ 1 defined and false ( ); 2 undefined; fallback value = " " [bad call: 1 defined twice]{{test-params||2=bar}}⇒ 1 defined and false (); 2 defined and true (bar); fallback value = ""{{test-params| |2=bar}}⇒ 1 defined and false ( ); 2 defined and true (bar); fallback value = " "
Mix of positional with non-whitespace values and empty/whitespace-only named
{{test-params|foo|2=}}⇒ 1 defined and true (foo); 2 defined and false (); fallback value = "foo"{{test-params|foo|2= }}⇒ 1 defined and true (foo); 2 defined and false (); fallback value = "foo"{{test-params|foo|2=bar}}⇒ 1 defined and true (foo); 2 defined and true (bar); fallback value = "foo"{{test-params|1=|bar}}⇒ 1 defined and true (bar); 2 undefined; fallback value = "bar" [bad call: 1 defined twice]{{test-params|1= |bar}}⇒ 1 defined and true (bar); 2 undefined; fallback value = "bar" [bad call: 1 defined twice]{{test-params|1=foo|bar}}⇒ 1 defined and true (bar); 2 undefined; fallback value = "bar" [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