Jump to content

Meta:Expansion demo templates

From Meta, a Wikimedia project coordination wiki

The expansion demo templates (see [1]) show, for documentation:

  • some wikitext
  • its rendering
  • a link to Special:ExpandTemplates applied to the wikitext; thus it shows:
    • The expanded wikitext; this differs from the wikitext if that contains the call of a variable, parser function or template. This is not provided by the similar Template:Demo inline (however, that has its own advantages).
    • The rendering of this expanded wikitext (the result of expanding again (bugzilla:28616), and further processing); this is typically the same as how the wikitext is rendered (the result of expanding once and further processing), because typically expanding again causes no further changes. However, there are exceptions; e.g. when calling Template:Txpd: {{txpd}} gives {{1x|p}} [2].

Usage:

{{xpdpattern|parameters}}
{{xpdpattern|l=x|parameters}} - with the template linked

The characters | and = and the double braces {{ and }} cannot be part of the parameters. Such components of the wikitext determine the pattern. Note that this does not apply for the colon, so e.g. in a parser function everything between the opening braces and the colon, such as #ifeq:abc, can be taken together as one parameter of xpdpattern, and the pattern is the same as when this is replaced by a template name.

The rendering is based on inline use of the supplied wikitext. To see the result when it is at the start of a new line, put a newline in the wikitext (in a separate parameter, see below). Thus "*a" → "*a" [3], but "
*a"
→ "

Optional parameter gives allows changing the arrow to some other wikitext, such as the word gives or the code for separating items in a table row.


Supported patterns

[edit]

Below are the supported patterns.[n 1] There is a separate template for each, because a combined template page gets slow due to all examples, while there is little advantage in having them combined.

pattern template 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
plain Template:Xpdplain " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 "
oc Template:Xpdoc " { { 1 } } 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 "
5oc Template:Xpd5oc " 1 2 3 4 5 { { 6 } } 7 8 9 10 11 12 13 14 15 16 "
10oc Template:Xpd10oc " 1 2 3 4 5 6 7 8 9 10 { { 11 } } 12 13 14 15 16 "
5o5c Template:Xpd5o5c " 1 2 3 4 5 { { 6 7 8 9 10 } } 11 12 13 14 15 16 "
opec Template:Xpdopec " { { 1 | 2 = 3 } } 4 5 6 7 8 9 10 11 12 13 14 "
5opec Template:Xpd5opec " 1 2 3 4 5 { { 6 | 7 = 8 } } 9 10 11 12 13 14 "
op3c Template:Xpdop3c " { { 1 | 2 3 4 } } 5 6 7 8 9 10 11 12 13 14 15 "
o5p5c Template:Xpdo5p5c " { { 1 2 3 4 5 | 6 7 8 9 10 } } 11 12 13 14 15 16 17 "
op3p3c Template:Xpdop3p3c " { { 1 | 2 3 4 | 5 6 7 } } 8 9 10 11 12 13 14 15 16 "
op14c Template:Xpdop14c " { { 1 | 2 3 4 5 6 7 8 9 10 11 12 13 14 15 } } "
op10pec Template:Xpdop10pec " { { 1 | 2 3 4 5 6 7 8 9 10 11 | 12 = 13 } } "
5op3c Template:Xpd5op3c " 1 2 3 4 5 { { 6 | 7 8 9 } } 10 11 12 13 14 15 "
op5cop5c Template:Xpdop5cop5c " { { 1 | 2 3 4 5 6 } } { { 7 | 8 9 10 11 12 } } "
oocc Template:Xpdoocc " { { 1 { { 2 } } } } 3 4 5 6 7 8 9 10 11 12 "
o5occ Template:Xpdo5occ " { { 1 2 3 4 5 { { 6 } } } } 7 8 9 10 11 12 "
opocc Template:Xpdopocc " { { 1 | { { 2 } } } } 3 4 5 6 7 8 9 10 11 "
op4oc5c Template:Xpdop4oc5c " { { 1 | 2 3 4 5 { { 6 } } 7 8 9 10 11 } } "
opopcc Template:Xpdopopcc " { { 1 | { { 2 | 3 } } } } 4 5 6 7 8 9 10 "
op3occ Template:Xpdop3occ " { { 1 | 2 3 4 { { 5 } } } } 6 7 8 9 10 "
opoppcc Template:Xpdopoppcc " { { 1 | { { 2 | 3 | 4 } } } } 5 6 7 8 9 "
5op3p3c Template:Xpd5op3p3c " 1 2 3 4 5 { { 6 | 7 8 9 | 10 11 12 } } 13 14 "
opepec Template:Xpdopepec " { { 1 | 2 = 3 | 4 = 5 } } 6 7 8 9 10 11 12 "
5opepec Template:Xpd5opepec " 1 2 3 4 5 { { 6 | 7 = 8 | 9 = 10 } } 11 12 "
op3pec Template:Xpdop3pec " { { 1 | 2 3 4 | 5 = 6 } } 7 8 9 10 11 12 "
5op3pec Template:Xpd5op3pec " 1 2 3 4 5 { { 6 | 7 8 9 | 10 = 11 } } 13 14 "
5op3p3p3c Template:Xpd5op3p3p3c " 1 2 3 4 5 { { 6 | 7 8 9 | 10 11 12 | 13 14 15 } } "
5op3p3pec Template:Xpd5op3p3pec " 1 2 3 4 5 { { 6 | 7 8 9 | 10 11 12 | 13 = 14 } } "
opepepec Template:Xpdopepepec " { { 1 | 2 = 3 | 4 = 5 | 6 = 7 } } 8 9 10 11 12 "
5opepepec Template:Xpd5opepepec " 1 2 3 4 5 { { 6 | 7 = 8 | 9 = 10 | 11 = 12 } } "

Naming scheme:

  • o = double opening braces
  • c = double closing braces
  • p = pipe character
  • e = equals sign
  • number = number of text parameters (at the start: instead of none; elsewhere: instead of one), to allow a space or newline as value of a separate parameter (this is required for proper processing)

Notes

[edit]
  1. In the case of a red link to the template the pattern is not yet supported.

Additional parameters

[edit]
  • The parameter gives can be assigned a text (or the empty string) to replace the word "gives", e.g. gives={{!}}{{!}} to produce "||", for use in a table.
  • By default quotation marks are used both around the wikitext and around the result. This is especially useful to show blank spaces and newlines. The optional parameter d can change this delimiter, e.g. "d=" to make it the empty string. Note however that the result including the delimiters is trimmed; this does not affect the default case, but may affect the result if the delimiter is empty, or if the delimiter itself has a newline or space at the start or end.

Comparison with Template:Demo inline

[edit]

Template:Demo inline is a single template, which additionally only requires Module:Demo, while the system of expansion demo templates explained on this page requires many separate templates, each for a particular pattern of wikitext, and, since the number of patterns is unlimited, this set does not cover all cases.

On the other hand, this system provides the intermediate result of expanded wikitext, through a link to a preloaded Special:ExpandTemplates page, which labels it as "Result", while its rendering is labeled "Preview". In the (simple) third and fourth example this "result" is ''26'', where the number is the day number within the month, for today.

Examples:

{{xpdoc|d=|CURRENTDAY}} {{CURRENTDAY}} → 26 [5]
{{xpdoc|CURRENTDAY}} "{{CURRENTDAY}}" → "26" [6]
{{xpdoc|d=''|CURRENTDAY}} ''{{CURRENTDAY}}''26 [7]
{{xpd5oc|d=|'|'||||CURRENTDAY|'|'}} ''{{CURRENTDAY}}''26 [8]
{{demo inline|<nowiki>{{CURRENTDAY}}</nowiki>}} {{CURRENTDAY}} → 26
{{demo inline|<nowiki>"{{CURRENTDAY}}"</nowiki>}} "{{CURRENTDAY}}" → "26"
{{demo inline|<nowiki>''{{CURRENTDAY}}''</nowiki>}} ''{{CURRENTDAY}}''26

Also, the system of expansion demo templates explained on this page allow the demonstration of the rendering of wikitext involving newlines or some special cases. For example they can show that a single newline is rendered as a space, while two newlines are rendered as a new paragraph (two newlines).

This system:

"a
b

c"
→ "a b

c" [9] (correct)

Template:Demo inline:

"a b c" → "a b

c" (the left-hand side is not according to the input)

Template:Demo:

"a
b

c"

"a

b

c" (the right-hand side is not how it is displayed)

(see also the edit box)

Examples

[edit]

Examples without additional parameters:

{{xpdoc|l=x|tss}} "{{tss}}" → "in" [10]
{{xpdoc|#expr:2*3}} "{{#expr:2*3}}" → "6" [11]
{{xpdoc|urlencode:!}} "{{urlencode:!}}" → "%21" [12]
{{xpdoc|uc:abc}} "{{uc:abc}}" → "ABC" [13]

If the left side contains an equals sign, the named parameter 1 can and should be used:

{{xpdoc|1=#expr:2+2=4}} "{{#expr:2+2=4}}" → "1" [14]

Equals sign in another parameter: see below.

Expression in double braces which itself includes a pair of double braces (simple nested expressions): use xpdoocc, with "|" for the inner "{{" and, if there is code after it, also for "}}". see also Template:Xpdn

{{xpdoocc|#expr:24*|CURRENTDAY|+3}} "{{#expr:24*{{CURRENTDAY}}+3}}" → "627" [15]
{{xpdoocc|#expr: 4*|#expr:not 0| >3}} "{{#expr: 4*{{#expr:not 0}} >3}}" → "1" [16]
{{xpdoocc|formatnum:|#expr:124*125}} "{{formatnum:{{#expr:124*125}}}}" → "15,500" [17]

Examples with additional parameters (no nesting possible):

{{xpd5op3p3c||||||#ifeq: 1.00|+1|okay}} "{{#ifeq: 1.00|+1|okay}}" → "okay" [18]
{{xpd|#ifeq: 1.00|+1|okay}} "{{#ifeq: 1.00|+1|okay}}" gives "okay" [19]
{{xpd|#ifeq: this|that||false|s=is}} "{{#ifeq: this|that||false}}" gives "false" [20]
{{xpdop3c|l=x|t1demo| |pqr| }} "{{t1demo| pqr }}" → "start- pqr -end" [21]
{{xpd5op3p3c|l=x||||||t2demo| |b| | |c| }} "{{t2demo| b|&s&sc }}" → "start- b -middle- c -end" [22]
{{xpd|l=x|t2demo| b | c }} "{{t2demo| b | c }}" gives "start- b -middle- c -end" [23]

For the examples below see also the edit box.

"abcdefghij" → "abcdefghij" [24]

"a
b

c"
→ "a b

c" [25]

"[[a]]b c" → "ab c" [26]

"[[a|b]] c" → "b c" [27]

"
{| class="wikitable sortable"
!pq!!rs
|-
|tu||vw
|-
|xyz||ab
|}"
→ "

pq rs
tu vw
xyz ab

" [28]

Limitations

[edit]

Characters in a separate parameter, and combinations of characters together in a separate parameter

[edit]

Some characters need to be in a separate parameter, and some combinations of characters need to be together in a separate parameter, so that the template can recognize them and handle them separately:

A newline needs to be in a separate parameter, otherwise it is displayed as a space, while the result takes into account the newline: {{xpdplain|a|



c}}

gives:

"a
b

c"
→ "a b

c" [29] (correct)

{{xpdplain|a
b

c}}

gives:

"a b c" → "a b

c" [30] (the resulting statement is with respect to the separation between a and b by itself correct, but not what was supposed to be demonstrated, and with respect to the separation between b and c even wrong)

Selection of pattern

[edit]

The expansion demo template does not check whether it is the correct one for the pattern of the wikitext. Assume that the wikitext which is the subject of the demonstration contains double braces, but that, due to separation with "|" and the wrong choice of the expansion demo template, they are treated as plain text in the expansion of the expansion demo template call. Then the produced message "..." → "..." is not correct, because in the expansion of the wikitext at the first set of dots the double braces would be interpreted as template braces etc. Example:

{{xpdplain|{|{tss}|}|}} gives:

"{{tss}}" → "{{tss}}" [31]

although in reality (as shown when selecting the correct expansion demo template xpdoc):

"{{tss}}" → "in" [32]

Additionally required templates

[edit]

In addition to any pattern template, required templates are:

Alphabetically:

In a tree structure:

Technical background

[edit]

Each template calls Template:Xpda. See there for more technical explanation.

Notes:

  • Some of the expansion demo templates still specify p=pattern in the call of Template:Xpda. However, this parameter is no longer used and can be removed.
  • Some of the expansion demo templates still specify gives={{{gives|gives}}} in the call of Template:Xpda. However, this is being replaced by gives= while moving the {{{gives|gives}}} to the start of result. This avoids trimming on the left side of the result if delimiter d is specified as the empty string; this trimming is especially inconvenient if the result is a table: quotes do not look good, but the newline before "{|" is needed.

See also

[edit]
  • Template:Tlx - produces just the template call, with the template name linked to the template, but not the result, e.g. "{{tlx|t2|b|c}}" gives "{{t2|b|c}}" [33]