User:Lakrits/sandbox3

From MozillaWiki
< User:Lakrits
Revision as of 16:02, 7 January 2015 by Lakrits (talk | contribs) (-)
Jump to navigation Jump to search

Usage

{{sp}} will insert a single, regular breaking space.

This template is especially useful when using parser functions, and you need to insert a space at the beginning or end of an optional string. Any space character at these positions, inserted using the space bar, is automatically trimmed. Use this template if you want the space to appear in the output.

Parameters

There are no parameters.

Syntax

The template's syntax is

  • {{sp}}

Examples

In parser functions

Consider the mock template {{Linguist}}. It has a fixed string, "He speaks Greek", and an optional sub-string, "and Latin", which is included if the parameter |latin is specified. The code for the template looks like this:

  • He speaks Greek{{#if:{{{latin|}}}|and Latin}}.

Inserted spaces in the examples below are coloured in Firefox light blue (see the Mozilla Style guide).

Adding a space using the space bar

1. No additional space

If the parameter |latin is left out, i.e. the template is called with the code string {{Linguist}}, the output looks alright (see the table below).

However, if the template is called with the code string {{Linguist|latin=yes}}, and no space at all is included in the template code, the words "Greek" and "and" will be merged into "Greekand".

Template code "latin" invoked Output
He speaks Greek{{#if:{{{latin|}}}|and Latin}}. No He speaks Greek.
Yes He speaks Greekand Latin.
2. Space inside function

Including a space inside the function will give the same output as in example 1, since (as mentioned above) all leading and trailing spaces are trimmed.

Template code "latin" invoked Output
He speaks Greek{{#if:{{{latin|}}}| and Latin}}. No He speaks Greek.
Yes He speaks Greekand Latin.
3. Space outside function

If the space instead is placed outside the function, the template will work when including the sub-string. But, if |latin is left out, this code will output a space between the last word and the full stop.

Template code "latin" invoked Output
He speaks Greek {{#if:{{{latin|}}}|and Latin}}. No He speaks Greek .
Yes He speaks Greek and Latin.

Adding a space using this template

4. {{sp}} inside function

Using this template to add a space before the optional string (i.e. {{sp}}and Latin), you will get the correct output both when leaving out |latin, and when it's invoked.

Template code "latin" invoked Output
He speaks Greek{{#if:{{{latin|}}}|{{sp}}and Latin}}. No He speaks Greek.
Yes He speaks Greek and Latin.

See also

  • {{nbsp}}: Inserts (up to 25) non-breaking space characters.
  • {{space}}: Inserts any one of a number of different space characters.