Help:Newlines and spaces

From Wikipedia, the free encyclopedia

This is a copy of the master help page at Meta. Do not edit this copy.
Edits will be lost in the next update from the master page. See below for more information.
See the bottom of this page for Wikipedia-specific help.
View this page using:
[http://en.wikipedia.org../../../../articles/n/e/w/Help%7ENewlines_and_spaces_a293.html
new preprocessor
diff

Two or more spaces are rendered as one; one newline is rendered as a space; two or more newlines are rendered as such:


p q  r   s    t
u

v


w

gives


p q r s t u

v


w


Contents

Stripping on save

In the case of one or more newlines at the start of the wikitext of a page, this number is reduced by one on every edit. Thus just pressing edit and save changes the page; this can be repeated, until no newline is left. Spaces (and hence lines with just spaces) are preserved.

Spaces and newlines at the end of the wikitext of the page are deleted right away on saving.

Thus a stored page has no newline at the end but may have one or more newlines at the start.

To preserve spaces and newlines at the start and end of the included part of a template, add a noinclude part (it can be dummy, in which case the pair of tags can be reduced to <noinclude />) at the start and/or end, see m:Template:t spaces (backlinks, edit) and m:Template:t newlines (backlinks, edit). Alternatively includeonly tags can be used.

Conversely, avoid an unwanted newline between the included part and the noinclude part.

Automatic newline at the start

Templates starting with "*", "#", or ":" automatically get a newline at the start:

"{{T ast}}" gives "Template:T ast" [1]Template:Xpd/

"{{T num}}" gives "Template:T num" [2]Template:Xpd/

"{{T colon}}" gives "Template:T colon" [3]Template:Xpd/

It is not prevented by a noinclude part at the start:

"{{T ast noinclude}}" gives "Template:T ast noinclude" [4]Template:Xpd/

Also for parser functions with the result starting with these characters:

pq{{#if:x|*}}r gives pq

  • r.

If this is not desired, as a workaround some unconditional text, if any, can be moved into the then- and else parts:

p{{#if:x|q*|q}}r gives pq*r.

Stripping on expansion

Spaces and newlines are stripped from:

Spaces and newlines are preserved at:

  • the start and end of the expanded wikitext of the value of an unnamed template parameter
  • the start and end of the expanded wikitext of the default value of a template parameter
  • the start and end of the expanded wikitext of a template

For example:

  • "{{1x|a}}" gives " a " [5]Template:Xpd/
  • {{1x|1= a }} gives "a" [6]

Preservation, of course, requires cumulative preservation. For example, using m:Template:nln (backlinks, edit) and m:Template:2x (backlinks, edit), p{{2x|{{nln}}}}q gives

p

q

due to the three preservation rules, while p{{2x|1={{nln}}}}q, p{{#if:x|{{2x|{{nln}}}}}}q, and p{{2x|{{#if:x|{{nln}}}}}}q give pq, pq, and pq, because in each case somewhere in the expansion chain the newlines are stripped. Similarly, using m:Template:spc (backlinks, edit), p{{2x|{{spc}}}}q gives p q, while p{{2x|1={{spc}}}}q, p{{#if:x|{{2x|{{spc}}}}}}q, and p{{2x|{{#if:x|{{spc}}}}}}q give pq, pq, and pq.

Spaces and/or newlines as value of an unnamed parameter

Using m:Template:1x (backlinks, edit), for fixed spaces and/or newlines {{1x| }}, {{1x|
}}, etc. can be used, or m:Template:spc (backlinks, edit) and m:Template:nln (backlinks, edit). Using m:Template:void (backlinks, edit), conditional spaces and/or newlines can be obtained by having instead of 1x a conditional template name which is either "1x" or "void".

See e.g. Template:Conditional table row Dutch municipality (backlinks, edit).

Spaces and/or newlines as parameter default

Alternatively, use a parameter (e.g. "undefined") which is known to be undefined, see the templates above and e.g. Template:Table row Dutch municipality (simple) (backlinks, edit). Unfortunately with substitution this does not allow producing a plain spaces and/or newlines in the wikitext; instead it gives somewhat cluttering code rendered like the spaces and/or newlines, see e.g. Template:For/aux/few#Substitution.

Conditional spaces and/or newlines can be obtained with this method by having in addition a parameter "empty" which has to be defined as empty parameter in the template call ("empty="): put a parameter with a conditional name at the end, with the spaces and/or newlines as default value; depending on the condition the name should either be "empty" or "undefined". In the latter case we get the spaces and/or newlines. See e.g. Template:Conditional table row Dutch municipality (backlinks, edit).

To avoid that an editor has to use the cryptic "empty=", the template call can be put in other template that transfers its parameters.

More examples

Examples using #if, m:Template:T1demo (backlinks, edit), m:Template:1x (backlinks, edit), and m:Template:strip (backlinks, edit):

a{{#if:1|
}}b

a{{#if:1|b
}}c

a{{#if:1|
b}}c

{{t1demo|
}}

{{t1demo|a
}}

{{t1demo|
a}}

a{{1x|
}}b

a{{1x|b
}}c

a{{1x|
b}}c

a{{strip|
}}b

a{{strip|b
}}c

a{{strip|
b}}c

{{t1demo|1=a
}}

{{t1demo|1=a
}}

{{t1demo|1=
a}}

a{{1x|1=
}}b

a{{1x|1=b
}}c

a{{1x|1=
b}}c

is expanded to

ab

abc

abc

start
end

starta
end

start
aend

a
b

ab
c

a
bc

ab

abc

abc

startaend

startaend

startaend

ab

abc

abc


 {{t1demo|1=     }}

gives

startend

{{t1demo|1=

}}

gives

startend.

Template with newline called inside parser function

As mentioned, if the template with the spaces and/or newlines at the end is called inside the expression for the value of an unnamed parameter of another template they are preserved. However, if the template with the spaces and/or newlines at the end is at the end of the then or else part of #if etc., or at the end of the expression for the value of a named parameter of another template, the spaces and/or newlines are stripped after all.

Using m:Template:tnl1 (backlinks, edit), m:Template:3x (backlinks, edit), and m:Template:for (backlinks, edit), compare:

{{tnl1}}d

{{3x|{{tnl1}}}}

{{3x|1={{tnl1}}}}

{{#if:x|{{tnl1}}}}d

{{for||call=tnl1|a|b}}

{{for||call=tnl1|a|b|nl=}}

expanding to

abc
d

abc
abc
abc


abcabcabc

abcd

abcabc

abcabc

Switch

*"{{#switch: p |p= q |r=s}}"
*"{{#switch:p| p = q |r=s}}"

gives

  • "q"
  • "q"

Thus, both from index values and from results, spaces are stripped.

As a workaround one can use parameter selection templates Pn:

"{{p{{#switch:p|p=1|r=2}}| q |s}}" gives " q "

This technique is used in m:Template:sw (backlinks, edit) (which can be extended to cover more cases):

"{{sw|p|p| q |r|s}}" gives " q ".

The allowed intermediate values are the positive integers for which there is a corresponding template Pn:

P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 P20 P21 P22 P23 P24 P25 P26 P27 P28 P29 P30 P31 P32 P33 P34 P35 P36 P37 P38 P39 P40 P41 P42 P43 P44 P45 P46 P47 P48 P49 P50 P51 P52 P53 P54 P55 P56 P57 P58 P59 P60 P61 P62 P63 P64 P65 P66 P67 P68 P69 P70 P71 P72 P73 P74 P75 P76 P77 P78 P79 P80 P81 P82 P83 P84 P85 P86 P87 P88 P89 P90 P91 P92 P93 P94 P95 P96 P97 P98 P99 P100

To avoid the need of many auxiliary templates, the switch functionality can also be obtained with nested parser functions #ifeq, and hence with m:Template:if (backlinks, edit).

Other parser functions

{{lc: AbC

dEf }} gives

"abc

def".


{{uc: AbC

dEf }} gives

"ABC

DEF".


{{lcfirst: AbC

dEf }} gives

"abC

dEf".


{{ucfirst: AbC

dEf }} gives

"AbC

dEf".


{{urlencode: AbC

dEf ghi}} gives

"AbC%0A%0AdEf+ghi".

Thus inner newlines become %0A and inner spaces become +.


{{anchorencode: AbC

dEf ghi}} gives

"AbC.0A.0AdEf_ghi".

Thus inner newlines become .0A and inner spaces become _.


"{{padleft: bc d|8|a}}" gives ""aaaabc d"" [7]

"{{padright: bc d|8|a}}" gives ""bc daaaa"" [8]


"{{formatnum: 12345 67890 }}" gives "12,345 67,890" [9]

Alternatives

Depending on the application an alternative for a pure newline or blank space in the expanded wikitext can be used, e.g.:

  • &nbsp;
  • <br>

See also


edit

Wikipedia-specific help



This page is a copy of the master help page at Meta (for general help information all Wikimedia projects can use), with two Wikipedia-specific templates inserted. To update the main text, edit the master help page for all projects at m:Help:Newlines and spaces. For Wikipedia-specific issues, use Template:Ph:Newlines and spaces (the extra text at the bottom of this page) or Template:Phh:Newlines and spaces for a Wikipedia-specific lead (text appears at the top of this page). You are welcome to replace the full wikitext of this page with that of the master page at Meta at any time. To view this page in other languages see the master page at Meta.