User:Ruakh/Help:Templates

Cases where whitespace is discarded
In a template-call, whitespace is discarded before and after the template-name, any parameter-names (or explicit parameter-numbers), and any named (or explicitly numbered) arguments. It is not, however, discarded before or after any positional (implicitly numbered) arguments. So, for example, {&#x7B; a | 1 = b | c = d &#x7D;} is equivalent to {&#x7B;a|1=b|c=d&#x7D;} and to {&#x7B;a|b|c=d&#x7D;}, but not to {&#x7B; a | b | c = d &#x7D;}.

In a parser-function call such as ucfirst: or #if:, whitspace is discarded after the initial {&#x7B;, and before and after every argument. The string ucfirst: or <tt>#if:</tt>, however, cannot be split up by whitespace. So, for example, <tt>{&#x7B; #if: a | b | c &#x7D;}</tt> is equivalent to <tt>{&#x7B;#if:a|b|c&#x7D;}</tt>, but <tt>{&#x7B; # if: a | b | c &#x7D;}</tt> or <tt>{&#x7B; #if : a | b | c &#x7D;}</tt> is broken.

When substituting a template or parser-function, whitespace is discarded before and after <tt>subst:</tt> or <tt>safesubst:</tt>, but is not allowed inside the <tt>subst:</tt> or <tt>safesubst:</tt>. So, for example, <tt>{&#x7B; subst: foo | 1 = bar | baz = bip &#x7D;}</tt> is fine, but <tt>{&#x7B; subst : foo | 1 = bar | baz = bip &#x7D;}</tt> is broken.

If a template contains something like <tt>{&#x7B; a | b = {&#x7B;{1}&#x7D;} &#x7D;}</tt> or <tt>{&#x7B; #if: a | {&#x7B;{1}&#x7D;} &#x7D;}</tt> — that is, if it uses a numbered argument <tt>{&#x7B;{1}&#x7D;}</tt> in a context where leading and trailing whitespace is discarded — then <tt>{&#x7B;{1}&#x7D;}</tt> will lose any leading or trailing whitespace. So, one could create a template <tt>{&#x7B;trim&#x7D;}</tt> with the contents <tt>{&#x7B; #if: 1 | {&#x7B;{1}&#x7D;} &#x7D;}</tt>, which would evaluate to a "trimmed" version of its argument. (Better yet, the contents <tt>{&#x7B; &lt;includeonly&gt;safesubst:&lt;/includeonly&gt; #if: 1 | {&#x7B;{1}&#x7D;} &#x7D;}</tt> would let <tt>{&#x7B;subst:trim&#x7D;}</tt> work as well.)

Whitespace is discarded before <tt>[&#x5B;Category:...&#x5D;]</tt>; for example, a&#xA; [[Category:b]]c will render as <tt>ac</tt>. (If not for the <tt>[&#x5B;Category:b&#x5D;]</tt>, the <tt>c</tt> would be inside <tt>&lt;pre&gt;</tt> tags.)

When normalizing a page-name in order to create a URL or link (in <tt>[&#x5B;...&#x5D;]</tt>, in <tt>{&#x7B;FULLURL&#x7D;}</tt>, and so on), whitespace is discarded from before and after the namespace, before and after the pagename, and after the fragment. It is not discarded, however, around <tt>/</tt>, nor after <tt>#</tt>. So, for example, <tt>[&#x5B; User : a #b &#x5D;]</tt> is equivalent to <tt>[&#x5B;User:a#b| User : a #b &#x5D;]</tt>.

If you need to insert a space that won't be discarded in a context where it normally would be, you can almost always write it as <tt>&amp;#32;</tt>. For example, <tt>({&#x7B; #if: {{&#x7B;{not|}&#x7D;} | not&amp;#32; &#x7D;}comparable)</tt> will produce either <tt>(comparable)</tt> or <tt>(not comparable)</tt>, as appropriate. Without the <tt>&amp;#32;</tt>, it could produce <tt>(notcomparable)</tt>, or it whitespace were inserted before the <tt>comparable</tt>, it could produce <tt>( comparable)</tt>. Alternatively, you can usually insert a <tt>&lt;nowiki/&gt;</tt> so that the space is internal. For example, <tt>({&#x7B; #if: {{&#x7B;{not|}&#x7D;} | not &lt;nowiki/&gt; &#x7D;}comparable)</tt> is usually equivalent to the above.

Collapsing linear whitespace
A sequence of multiple spaces is equivalent to a single space, except in preformatted text. (This is true in HTML and in wikitext.) If you need to force the display of multiple spaces, you can replace some of them with <tt>&amp;nbsp;</tt> (non-breaking spaces).

Line-breaks
The edit-window initially includes a single trailing line-break that is not actually part of whatever page or section you are editing. Removing this line-break, or leaving it in place, has no effect. (But adding anything after the line-break, of course, turns it into a real line-break. For example, adding <tt>&lt;noinclude&gt;...&lt;/noinclude&gt;</tt> on its own line means that a line-break is added to the page.)

A single line-break by itself has no effect, except in preformatted text, or at the end of a line that starts with <tt>*</tt> or <tt>#</tt> or <tt>:</tt> or <tt>;</tt>. However, it's not as easy as it might sound to make sure that a given line-break is indeed "a single line-break by itself". If it comes right before or after <tt>{&#x7B;{1}&#x7D;}</tt>, or right at the beginning or end of the template (after discarding non-included sections), then it risks combining with wikitext elsewhere, and suddenly having an effect. So, some caution is in order.

To create a visual line-break, you can use <tt>&lt;br /&gt;</tt>, except inside <tt>&lt;pre&gt;</tt> tags, where you can use <tt>&amp;#xA;</tt>. (Of course, inside <tt>&lt;pre&gt;</tt> tags this isn't usually necessary. And templates don't usually contain <tt>&lt;pre&gt;</tt> tags, anyway!)

Blank vs. missing arguments
A blank/empty argument is not automatically the same as a missing/unspecified one. For example, neither <tt>{&#x7B; a | b = &#x7D;}</tt> nor <tt>{&#x7B; a |&#x7D;}</tt> is necessarily equivalent to <tt>{&#x7B; a &#x7D;}</tt>.

The following evaluates to <tt>nonblank</tt>, <tt>blank</tt>, or <tt>missing</tt> if the parameter <tt>a=...</tt> is (respectively) non-blank, explicitly set to blank, or missing: (The use of both <tt>{&#x7B;{a|+}&#x7D;}</tt> and <tt>{&#x7B;{a|-}&#x7D;}</tt> allows a missing/unspecified <tt>a=</tt> to be distinguished from both <tt>a=-</tt> and <tt>a=+</tt>.)

However, if a parameter is optional, it is frequently a good idea to treat a blank/empty value and a missing/unspecified value the same way. For example, something like <tt>{&#x7B;{a|b}&#x7D;}</tt> might be better written as <tt>{&#x7B; #if: {&#x7B;{a|}&#x7D;} | {&#x7B;{a}&#x7D;} | b &#x7D;}</tt>. This can make a template easier for bots, scripts, and other templates to make use of. (It is also likely to be more intuitive to human users.)

Parser-functions
For information on parser-functions, see mw:Help:Magic Words and mw:Help:Extension:ParserFunctions.

Numbered and named parameters
[TODO]

Templates vs. template-pages
[TODO]

Inclusion, substitution, and safe-substitution
[TODO]