User:Ruakh/Help:Templates

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Whitespace[edit]

Cases where whitespace is discarded[edit]

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, {{ a | 1 = b | c = d }} is equivalent to {{a|1=b|c=d}} and to {{a|b|c=d}}, but not to {{ a | b | c = d }}.

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

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

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

Whitespace is discarded before [[Category:...]]; for example,

a
 [[Category:b]]c

will render as ac. (If not for the [[Category:b]], the c would be inside <pre> tags.)

When normalizing a page-name in order to create a URL or link (in [[...]], in {{FULLURL}}, 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 /, nor after #. So, for example, [[ User : a #b ]] is equivalent to [[User:a#b| User : a #b ]].

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 &#32;. For example, ({{ #if: {{{{not|}}} | not&#32; }}comparable) will produce either (comparable) or (not comparable), as appropriate. Without the &#32;, it could produce (notcomparable), or it whitespace were inserted before the comparable, it could produce ( comparable). Alternatively, you can usually insert a <nowiki/> so that the space is internal. For example, ({{ #if: {{{{not|}}} | not <nowiki/> }}comparable) is usually equivalent to the above.

Collapsing linear whitespace[edit]

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 &nbsp; (non-breaking spaces).

Line-breaks[edit]

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 <noinclude>...</noinclude> 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 * or # or : or ;. 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 {{{1}}}, 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 <br />, except inside <pre> tags, where you can use &#xA;. (Of course, inside <pre> tags this isn't usually necessary. And templates don't usually contain <pre> tags, anyway!)

Blank vs. missing arguments[edit]

A blank/empty argument is not automatically the same as a missing/unspecified one. For example, neither {{ a | b = }} nor {{ a |}} is necessarily equivalent to {{ a }}.

The following evaluates to nonblank, blank, or missing if the parameter a=... is (respectively) non-blank, explicitly set to blank, or missing:

{{ #switch: {{{a|+}}} {{{a|-}}} | + - = missing | = blank | #default = nonblank }}

(The use of both {{{a|+}}} and {{{a|-}}} allows a missing/unspecified a= to be distinguished from both a=- and a=+.)

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 {{{a|b}}} might be better written as {{ #if: {{{a|}}} | {{{a}}} | b }}. 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[edit]

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

Numbered and named parameters[edit]

[TODO]

Templates vs. template-pages[edit]

[TODO]

Inclusion, substitution, and safe-substitution[edit]

[TODO]