Help:Template: Difference between revisions

From Alpine Linux
(Cancelled REDIRECT, added content)
(Update note about ParserFunctions)
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
This page is more about designing templates than using them. For details of how to use existing templates, see [[Project:Markup syntax]] or the documentation for [[Special:AllPages/Template:|any template]].
This page is about designing templates more than using them. For details of how to use existing templates, see [[Help:Editing]] or the documentation for [[Special:AllPages/Template:|any template]].




== Including ==
To include a template---or any page---in another page:
To include a template---or any page---in another page:


* <code>{{lb}}T{{rb}}</code> includes [[Template:T]]
* <code>{{lb}}T{{rb}}</code> includes [[Template:T]]
* <code>{{lb}}:Page name{{rb}}</code> includes [[Page name]]
* <code>{{lb}}<b>:</b>Page name{{rb}}</code> includes [[Page name]]
* <code>{{lb}}subst:T{{rb}}</code> or <code>{{lb}}subst::Page name{{rb}}</code> are instead replaced by the current contents of [[Template:T]] or [[Page name]]
* <code>{{lb}}subst:T{{rb}}</code> or <code>{{lb}}subst<b>::</b>Page name{{rb}}</code> are instead replaced by the current contents of [[Template:T]] or [[Page name]]
* <code>{{lb}}msgnw:T{{rb}}</code> includes [[Template:T] in <code>&lt;nowiki></code> style
* <code>{{lb}}msgnw:T{{rb}}</code> includes [[Template:T]] in <code>&lt;nowiki></code> style




Templates have a limited condition-testing functionality. The following were based on code from the MediaWiki manuals:
== If-templates ==
Templates have a limited condition-testing functionality. The following are based on code from the MediaWiki manuals:


* {{lb}}Ifn|value|result{{rb}} works like shell's <code>if [ -n "value" ]; then result; fi</code>
<dl>
* {{lb}}Ifz|value|result{{rb}} works like shell's <code>if [ -z "value" ]; then result; fi</code>
<dt>{{lb}}Ifn|<var>value</var>|<var>result</var>{{rb}}
* {{lb}}If|value|resultyes|resultno{{rb}} works like shell's <code>if [ -n "value"]; then resultyes; else resultno; fi</code>
<dd>
* {{lb}}Ifeq|value|test|resultyes|resultno{{rb}} works like shell's <code>if ["value" = test ]; then resultyes; else resultno; fi</code>
Yields <var>result</var> if <var>value</var> isn't blank.
(Compare shell's <code>if [ -n "value" ]; then result; fi</code>)
One limitation: {{lb}}ifn|2=<var>result</var>{{rb}} yields <var>result</var> (but works properly if <var>value</var> is defined and empty).


======
<dt>{{lb}}Ifz|<var>value</var>|<var>result</var>{{rb}}
<nowiki>{{</nowiki>if|<var>value</var>|<var>then</var>|<var>else</var>}} yields <var>then</var> if <var>value</var> isn't blank; otherwise it yields <var>else</var>.
<dd>
One limitation: <nowiki>{{</nowiki>if|2=<var>then</var>}} yields <var>then</var> (but works properly if <var>value</var> is defined and empty, or <var>else</var> is defined).
Yields <var>result</var> if <var>value</var> is blank.
(Compare shell's <code>if [ -z "value" ]; then result; fi</code>)


<nowiki>{{</nowiki>ifz|<var>value</var>|<var>result</var>}} yields <var>result</var> if <var>value</var> is blank.
<dt>{{lb}}If|<var>value</var>|<var>resultyes</var>|<var>resultno</var>{{rb}}
<dd>
Yields <var>resultyes</var> if <var>value</var> isn't blank; otherwise it yields <var>resultno</var>.
(Compare shell's <code>if [ -n "value"]; then resultyes; else resultno; fi</code>)
One limitation: {{lb}}if|2=<var>resultyes</var>{{rb}} yields <var>resultyes</var> (but works properly if <var>value</var> is defined and empty, or <var>resultno</var> is defined).


<nowiki>{{</nowiki>ifn|<var>value</var>|<var>result</var>}} yields <var>result</var> if <var>value</var> isn't blank.
<dt>{{lb}}Ifeq|<var>value</var>|<var>test</var>|<var>resultyes</var>|<var>resultno</var>{{rb}}
One limitation: <nowiki>{{</nowiki>ifn|2=<var>result</var>}} yields <var>result</var> (but works properly if <var>value</var> is defined and empty).
<dd>
Yields <var>resultyes</var> if <var>value</var> equals <var>test</var>; otherwise it yields <var>resultno</var>.
(Compare shell's <code>if ["value" = test ]; then resultyes; else resultno; fi</code>)
</dl>


<nowiki>{{</nowiki>ifeq|<var>value</var>|<var>test</var>|<var>then</var>|<var>else</var>}} yields <var>then</var> if <var>value</var> equals <var>test</var>; otherwise it yields <var>else</var>.
Now that we've upgraded to MediaWiki >= 1.19.1, though (currently at 1.26.2), we <em>would</em> be able to use {{lb}}#if:<var>value</var>|<var>result</var>|<var>resultno</var>{{rb}} and {{lb}}#ifeq:<var>value</var>|<var>test</var>|<var>resultyes</var>|<var>resultno</var>{{rb}}, which don't suffer the above limitations. Here are [http://www.mediawiki.org/wiki/Help:Extension:ParserFunctions more details.] However, it seems that won't work until the ParserFunctions extension [[Special:Version|is enabled]].
=====


== Template arguments ==


In the template's expansion markup, <code><nowiki>{{{</nowiki>1}}}</code> will expand to the first (implicitly named) argument, and <code><nowiki>{{{</nowiki>2|<var>default</var>}}}</code> will expand to the second but default to <var>default</var> if the second argument is not supplied. Note that if the template is invoked as <code>{{lb}}Foo|arg1|{{rb}}</code>, then the second argument ''has'' been supplied; here it's the empty string.


Arguments can also be explicitly named/numbered, as when you invoke <code>{{lb}}Foo|bar=arg1|2={{rb}}</code>. Arguments supplied in these ways have leading and trailing whitespace trimmed. (Whitespace is preserved when arguments are only implicitly named.)


Skeleton for a new template:
To suppress expansion of an argument's contents, wrap <code>{{{<var>argument</var>}}}</code> with <code>{{lb}}#tag:nowiki|...{{rb}}</code>; see [[Template:Cat]] for an example.
 
Inside a template's expansion markup, substrings beginning with <code>*</code>, <code>#</code>, <code>:</code>, or <code>;</code> are automatically prefixed by a
newline. Use <code>start<nowiki>{{{</nowiki><var>dummy</var>|<b>;</b>}}}finish</code> to generate <code>start<b>;</b>finish</code>. (The empty string may be used for <var>dummy</var>.)
 
<code>{{lb}}ns:0{{rb}}</code> expands to the empty string.
 
If your template arguments are unexpectedly expanding as {{{1}}}, then you neglected to supply the argument; or you supplied text that the wiki markup engine is interpreting in a way you didn't expect. You can stop this from happening by surrounding the relevant text with <code>&lt;nowiki></code> tags. For example, instead of:
 
<pre>{{Cmd|export PS1="(chroot)$PS1"}}</pre>
 
use:
 
<pre>{{Cmd|&lt;nowiki>export PS1="(chroot)$PS1"&lt;/nowiki>}}</pre>
 
A shorter alternative is to explicitly name the first argument:
 
<pre>{{Cmd|1=export PS1="(chroot)$PS1}"}}</pre>
 
See also [[Help:Editing#cat|the escape characters for {{lb}}Cat{{rb}}]], which can be used inside any template invocation.
 
== Skeleton for a new template ==


<pre><noinclude>{{Template}}
<pre><noinclude>{{Template}}
Line 58: Line 93:
</pre>
</pre>


=== Misc ===
<pre>
markup ... {{{1}}} {{{2|default}}} ...
{{T|1=a=alpha|reason=...}}
  {{{1|default}}}
  {{{reason}}}
Explicitly named/numbered args are trimmed, to preserve whitespace have to use implicitly numbered args.
Inside a template, substrings beginning with "*", "#", ":", or ";" are automatically prefixed by a
newline. Use `a{{{dummy|;}}}b` to generate `a;b`. (`dummy` could be the empty string.)
Use {{ns:0}} for empty string.
</pre>
[[Template:Cat]] uses <nowiki>{{#tag:nowiki|...}}</nowiki> around its <nowiki>{{{2}}}</nowiki>.




[[Category:Wiki]]
[[Category:Wiki]]

Latest revision as of 11:25, 30 May 2017

This page is about designing templates more than using them. For details of how to use existing templates, see Help:Editing or the documentation for any template.


Including

To include a template---or any page---in another page:


If-templates

Templates have a limited condition-testing functionality. The following are based on code from the MediaWiki manuals:

{{Ifn|value|result}}
Yields result if value isn't blank. (Compare shell's if [ -n "value" ]; then result; fi) One limitation: {{ifn|2=result}} yields result (but works properly if value is defined and empty).
{{Ifz|value|result}}
Yields result if value is blank. (Compare shell's if [ -z "value" ]; then result; fi)
{{If|value|resultyes|resultno}}
Yields resultyes if value isn't blank; otherwise it yields resultno. (Compare shell's if [ -n "value"]; then resultyes; else resultno; fi) One limitation: {{if|2=resultyes}} yields resultyes (but works properly if value is defined and empty, or resultno is defined).
{{Ifeq|value|test|resultyes|resultno}}
Yields resultyes if value equals test; otherwise it yields resultno. (Compare shell's if ["value" = test ]; then resultyes; else resultno; fi)

Now that we've upgraded to MediaWiki >= 1.19.1, though (currently at 1.26.2), we would be able to use {{#if:value|result|resultno}} and {{#ifeq:value|test|resultyes|resultno}}, which don't suffer the above limitations. Here are more details. However, it seems that won't work until the ParserFunctions extension is enabled.

Template arguments

In the template's expansion markup, {{{1}}} will expand to the first (implicitly named) argument, and {{{2|default}}} will expand to the second but default to default if the second argument is not supplied. Note that if the template is invoked as {{Foo|arg1|}}, then the second argument has been supplied; here it's the empty string.

Arguments can also be explicitly named/numbered, as when you invoke {{Foo|bar=arg1|2=}}. Arguments supplied in these ways have leading and trailing whitespace trimmed. (Whitespace is preserved when arguments are only implicitly named.)

To suppress expansion of an argument's contents, wrap {{{argument}}} with {{#tag:nowiki|...}}; see Template:Cat for an example.

Inside a template's expansion markup, substrings beginning with *, #, :, or ; are automatically prefixed by a newline. Use start{{{dummy|;}}}finish to generate start;finish. (The empty string may be used for dummy.)

{{ns:0}} expands to the empty string.

If your template arguments are unexpectedly expanding as {{{1}}}, then you neglected to supply the argument; or you supplied text that the wiki markup engine is interpreting in a way you didn't expect. You can stop this from happening by surrounding the relevant text with <nowiki> tags. For example, instead of:

{{Cmd|export PS1="(chroot)$PS1"}}

use:

{{Cmd|<nowiki>export PS1="(chroot)$PS1"</nowiki>}}

A shorter alternative is to explicitly name the first argument:

{{Cmd|1=export PS1="(chroot)$PS1}"}}

See also the escape characters for {{Cat}}, which can be used inside any template invocation.

Skeleton for a new template

<noinclude>{{Template}}
Short description.

=== Usage ===
Longer description.

<pre>{{Foo|arg}}</pre>

Further comments.

=== Example ===

<pre>{{Foo|arg}}</pre>

will produce:
{{Foo|arg}}

=== See also ===

* [[:Template:Bar]]

</noinclude><includeonly>EXPANSION</includeonly>