Community Central
Community Central
(interlang)
Tags: Help sourceedit
m (Interlanguage link: +fr.)
Tag: Source edit
(19 intermediate revisions by 17 users not shown)
Line 1: Line 1:
[[Help:Templates|Templates]] allow you to pass '''parameters''' that can be used to affect the content or design of the template.
+
[[Help:Templates|Templates]] allow you to pass '''parameters'''. These are used to alter the way the template is displayed, such as including specific text or altering the design.
   
When inserting templates on pages, the [[Help:Editing|visual editor]] allows you to define and insert parameter text without using [[Help:Wikitext|wikitext]].
+
Note: this page describes manipulating templates in [[Help:Source editor|source editor]]. When inserting templates on pages, the [[Help:VisualEditor|VisualEditor]] can allow you to define and insert parameter text without touching [[Help:Wikitext|wikitext]].
   
==Simple parameters==
+
== Simple parameters ==
  +
For small templates, using basic parameters without specific names to alter a template will often suffice. These parameters are numbered rather than named, using <code><nowiki>{{{1}}}</nowiki></code>, <code><nowiki>{{{2}}}</nowiki></code>, <code><nowiki>{{{3}}}</nowiki></code>, etc.
* Create a page on your wikia called "Template:Box":
 
  +
  +
=== Guide ===
  +
This guide will walk through creating a basic box template with simple parameters:
  +
* Create a page on your wiki with a basic name (for instance, "Template:Box"):
 
* Add the following code to that page:
 
* Add the following code to that page:
   
<pre><div style="width:10em; height:5em; border:1px solid #999; background-color:#CDF; color:#000; text-align:center; padding-top:2em;">
+
<pre><div style="background-color: #CDF; border: 1px solid #999; color: black; height: 5em; padding-top: 2em; text-align: center; width: 10em;">
 
{{{1}}}
 
{{{1}}}
 
</div></pre>
 
</div></pre>
 
* Save the page to see that it displays the following content:
 
 
: <div style="background-color: #CDF; border: 1px solid #999; color: black; height: 5em; padding-top: 2em; text-align: center; width: 10em;">{{{1}}}</div>
* Save the page and note that it displays the following content:
 
 
* Create an article page on your wiki called <code>Template test</code>
 
:<div style="width:10em; height:5em; border:1px solid #999; background-color:#CDF; color:#000; text-align:center; padding-top:2.5em;">{{{1}}}</div>
 
 
* Create an article page on your wikia called <code>Template test</code>
 
 
* Enter the following code on that page to display your template:
 
* Enter the following code on that page to display your template:
 
: <code><nowiki>{{Box|Example text}}</nowiki></code>
 
:<code><nowiki>{{Box|Example text}}</nowiki></code>
 
 
 
* Save the page and note that it displays the following content:
 
* Save the page and note that it displays the following content:
 
: <div style="background-color: #CDF; border: 1px solid #999; color: black; height: 5em; padding-top: 2em; text-align: center; width: 10em;">Example text</div>
   
 
=== Understanding what happened ===
:<div style="width:10em; height:5em; border:1px solid #999; background-color:#CDF; color:#000; text-align:center; padding-top:2.5em;">Example text</div>
 
 
===Understanding what happened===
 
 
This replacement happened because <code><nowiki>{{{1}}}</nowiki></code> tells the wiki to pass the first parameter of the template here. This can be extended with <code><nowiki>{{{2}}}</nowiki></code>, <code><nowiki>{{{3}}}</nowiki></code>, etc.
 
This replacement happened because <code><nowiki>{{{1}}}</nowiki></code> tells the wiki to pass the first parameter of the template here. This can be extended with <code><nowiki>{{{2}}}</nowiki></code>, <code><nowiki>{{{3}}}</nowiki></code>, etc.
   
 
The number represents the number of the parameter:
 
The number represents the number of the parameter:
:<code><nowiki>{{box|first parameter|second parameter|third parameter}}</nowiki></code>
+
: <code><nowiki>{{box|first parameter|second parameter|third parameter}}</nowiki></code>
 
If you wish not to use a certain parameter, you can leave it blank, but the "<code>|</code>" character must still be included. For example:
 
: <code><nowiki>{{box|first parameter||third parameter}}</nowiki></code>
   
 
== Named and default parameters ==
If you wish not to use a certain parameter, you can leave it blank, but the '|' character must still be included. For example:
 
  +
For more complex templates, it is often better to use named parameters. This allows for more freedom in how templates are created, and also makes templates easier to use as users will know which parameter affects which element.
:<code><nowiki>{{box|first parameter||third parameter}}</nowiki></code>
 
 
==Named and default parameters==
 
As an alternative to the above, you can use named parameters. Though this makes the code slightly more complex, it allows more freedom in how templates are created.
 
   
  +
=== Guide ===
  +
This guide will walk through using named parameters to the above "Template:Box" example:
 
* Using the same example pages as before, edit "Template:Box" and replace the content with the following code:
 
* Using the same example pages as before, edit "Template:Box" and replace the content with the following code:
   
 
<pre><div style="background-color: {{{bgcolor|#CDF}}}; border: 1px solid #999; color: {{{textcolor|black}}}; height: 5em; padding-top: 2em; text-align: center; width: 10em;">
<pre>
 
<div style="width:10em; height:5em; border:1px solid #999; background-color:{{{bgcolor|#CDF}}}; color:{{{textcolor|#000}}}; text-align:center; padding-top:2em;">
 
 
{{{text}}}
 
{{{text}}}
</div>
+
</div></pre>
 
: <code><nowiki>{{{text}}}</nowiki></code> introduces the concept of a '''named parameter'''. <code><nowiki>{{{bgcolor|#CDF}}}</nowiki></code> also introduces the concept of a '''default parameter''': if "bgcolor" is not defined, "<code>#CDF</code>" will be used.
</pre>
 
 
: <code><nowiki>{{{text}}}</nowiki></code> introduces the concept of a '''named parameter'''. <code><nowiki>{{{bgcolor|#CDF}}}</nowiki></code> also introduces the concept of a '''default parameter''': if 'bgcolor' is not defined, '#CDF' will be used.
 
 
 
* Edit the page "Template test" and replace it with the following code:
 
* Edit the page "Template test" and replace it with the following code:
:<code><nowiki>{{box|bgcolor=navy|textcolor=white|text=A navy blue box}}</nowiki></code>
+
: <code><nowiki>{{box|bgcolor=navy|textcolor=white|text=A navy blue box}}</nowiki></code>
 
* Save your page, and note that it displays the following:
 
* Save your page, and note that it displays the following:
 
: <div style="background-color: navy; border: 1px solid #999; color: white; height: 5em; padding-top: 2em; text-align: center; width: 10em;">A navy blue box</div>
   
 
=== Understanding what happened ===
:<div style="width:10em; height:5em; border:1px solid #999; background-color:navy; color:white; text-align:center; padding-top:2em;">A navy blue box</div>
 
 
===Understanding what happened===
 
 
As the parameters have names, you can pass them in any order, so <code><nowiki>{{box|textcolor=white|text=A navy blue box|bgcolor=navy}}</nowiki></code> would produce an identical box.
 
As the parameters have names, you can pass them in any order, so <code><nowiki>{{box|textcolor=white|text=A navy blue box|bgcolor=navy}}</nowiki></code> would produce an identical box.
   
 
Due to default parameters, if, say, the background color was not defined - as in <code><nowiki>{{box|textcolor=white|text=A navy blue box}}</nowiki></code> - you would get:
 
Due to default parameters, if, say, the background color was not defined - as in <code><nowiki>{{box|textcolor=white|text=A navy blue box}}</nowiki></code> - you would get:
 
: <div style="background-color: #CDF; border: 1px solid #999; color: white; height: 5em; padding-top: 2em; text-align: center; width: 10em;">A navy blue box</div>
 
 
Named parameters are frequently written on separate lines or with space between parameter name and "=" sign, to aid readability. Generally, this is done in a big template composing of many parameters. It is not unusual to see them written in this form:
:<div style="width:10em; height:5em; border:1px solid #999; background-color:#CDF; color:white; text-align:center; padding-top:2em;">A navy blue box</div>
 
 
Named parameters are frequently written on separate lines to aid readability. It is not unusual to see them written in this form:
 
   
 
<pre>
 
<pre>
Line 68: Line 60:
 
}}
 
}}
 
</pre>
 
</pre>
 
Default parameter can be left blank if you want an optional parameter not to insert anything on a page.
   
 
For example, if you wrote <code><nowiki>{{{text}}}</nowiki></code> in a template, but did not include a "<code>text=</code>" input on the article page, it would show up as "{{{text}}}". However, if you wrote <code><nowiki>{{{text|}}}</nowiki></code> in the template, the default is no text, so nothing would show up on the article page.
   
  +
It is also possible to set another parameter as the default of a parameter, like <code><nowiki>{{{text|{{{1|}}}}}}</nowiki></code>. This way, if an editor uses <code><nowiki>{{box|Example text}}</nowiki></code>, they will get the same output as <code><nowiki>{{box|text=Example text}}</nowiki></code>. This means that a template will default to the <code><nowiki>|text=</nowiki></code> parameter, but if it cannot find it, it will use the unnamed parameter instead; if it cannot find ''that'', it will use no text.
Default parameter can be left blank if you want an optional parameter not to insert anything on a page:
 
   
 
== See also ==
For example, if you wrote <code><nowiki>{{{text}}}</nowiki></code> in a template, but did not include a 'text=' input on the article page, it would show up as "{{{text}}}". However, if you wrote <code><nowiki>{{{text|}}}</nowiki></code> in the template, the default is no text, so nothing would show up on the article page.
 
 
* [[wikipedia:Help:Template#Parameters|More detailed template parameter help on Wikipedia]]
 
==See also==
 
* {{#NewWindowLink:wikipedia:Help:Template#Parameters|More detailed Parameter help on Wikipedia}}
 
 
* [[Help:Templates|Templates]]
 
* [[Help:Templates|Templates]]
* [[Help:Infoboxes]] and [[User blog:Rappy 4187/Guide to Infoboxes|Rappy 4187's guide to infoboxes]]
+
* [[Help:Infoboxes|Infoboxes]]
 
* [[Help:Parser functions|Parser functions]]
 
* [[Help:Parser functions|Parser functions]]
 
[[Category:Editing]]
 
[[Category:Help]]
 
   
 
[[de:Hilfe:Vorlagen/erstellen#Parameter]]
 
[[de:Hilfe:Vorlagen/erstellen#Parameter]]
[[es:Ayuda:Plantillas/Parámetros]]
+
[[es:Ayuda:Parámetros de plantillas]]
[[fi:Ohje:Mallineiden parametrit]]
+
[[fr:Aide:Paramètres de modèle]]
[[it:Aiuto:Template/Parametri]]
+
[[it:Aiuto:Parametri dei template]]
 
[[ja:ヘルプ:テンプレートパラメータ]]
 
[[ja:ヘルプ:テンプレートパラメータ]]
  +
[[ko:도움말:틀 매개변수]]
  +
[[pl:Pomoc:Parametry szablonów]]
  +
[[pt:Ajuda:Parâmetros de predefinição]]
 
[[ru:Справка:Параметры шаблонов]]
 
[[ru:Справка:Параметры шаблонов]]
  +
[[tr:Yardım:Şablon parametreleri]]
[[zh:Help:模板参数]]
 
 
[[vi:Trợ giúp:Bản mẫu chứa thông số]]
 
[[vi:Trợ giúp:Bản mẫu chứa thông số]]
 
[[zh:Help:模板参数]]
  +
 
[[Category:Help]]
 
[[Category:Editing]]
  +
[[Category:Tables & templates]]

Revision as of 00:08, 16 February 2021

Templates allow you to pass parameters. These are used to alter the way the template is displayed, such as including specific text or altering the design.

Note: this page describes manipulating templates in source editor. When inserting templates on pages, the VisualEditor can allow you to define and insert parameter text without touching wikitext.

Simple parameters

For small templates, using basic parameters without specific names to alter a template will often suffice. These parameters are numbered rather than named, using {{{1}}}, {{{2}}}, {{{3}}}, etc.

Guide

This guide will walk through creating a basic box template with simple parameters:

  • Create a page on your wiki with a basic name (for instance, "Template:Box"):
  • Add the following code to that page:
<div style="background-color: #CDF; border: 1px solid #999; color: black; height: 5em; padding-top: 2em; text-align: center; width: 10em;">
{{{1}}}
</div>
  • Save the page to see that it displays the following content:
{{{1}}}
  • Create an article page on your wiki called Template test
  • Enter the following code on that page to display your template:
{{Box|Example text}}
  • Save the page and note that it displays the following content:
Example text

Understanding what happened

This replacement happened because {{{1}}} tells the wiki to pass the first parameter of the template here. This can be extended with {{{2}}}, {{{3}}}, etc.

The number represents the number of the parameter:

{{box|first parameter|second parameter|third parameter}}

If you wish not to use a certain parameter, you can leave it blank, but the "|" character must still be included. For example:

{{box|first parameter||third parameter}}

Named and default parameters

For more complex templates, it is often better to use named parameters. This allows for more freedom in how templates are created, and also makes templates easier to use as users will know which parameter affects which element.

Guide

This guide will walk through using named parameters to the above "Template:Box" example:

  • Using the same example pages as before, edit "Template:Box" and replace the content with the following code:
<div style="background-color: {{{bgcolor|#CDF}}}; border: 1px solid #999; color: {{{textcolor|black}}}; height: 5em; padding-top: 2em; text-align: center; width: 10em;">
{{{text}}}
</div>
{{{text}}} introduces the concept of a named parameter. {{{bgcolor|#CDF}}} also introduces the concept of a default parameter: if "bgcolor" is not defined, "#CDF" will be used.
  • Edit the page "Template test" and replace it with the following code:
{{box|bgcolor=navy|textcolor=white|text=A navy blue box}}
  • Save your page, and note that it displays the following:
A navy blue box

Understanding what happened

As the parameters have names, you can pass them in any order, so {{box|textcolor=white|text=A navy blue box|bgcolor=navy}} would produce an identical box.

Due to default parameters, if, say, the background color was not defined - as in {{box|textcolor=white|text=A navy blue box}} - you would get:

A navy blue box

Named parameters are frequently written on separate lines or with space between parameter name and "=" sign, to aid readability. Generally, this is done in a big template composing of many parameters. It is not unusual to see them written in this form:

{{box
 |bgcolor   = navy
 |textcolor = white
 |text      = A navy blue box
}}

Default parameter can be left blank if you want an optional parameter not to insert anything on a page.

For example, if you wrote {{{text}}} in a template, but did not include a "text=" input on the article page, it would show up as "{{{text}}}". However, if you wrote {{{text|}}} in the template, the default is no text, so nothing would show up on the article page.

It is also possible to set another parameter as the default of a parameter, like {{{text|{{{1|}}}}}}. This way, if an editor uses {{box|Example text}}, they will get the same output as {{box|text=Example text}}. This means that a template will default to the |text= parameter, but if it cannot find it, it will use the unnamed parameter instead; if it cannot find that, it will use no text.

See also