Community Central
Community Central
m (Reverted edits by 74.105.121.132 (wall | block) to last version by Leviathan 89)
(Update)
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.
__NOTOC__ __NOEDITSECTION__
 
Templates allow you to pass '''parameters''' that can be used to affect the content or design of the template.
 
   
  +
==Simple parameters==
==Step-by-Step==
 
 
* Create a page on your wikia called "Template:Box":
===''Example of Simple Replacement''===
 
  +
* 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;">
* Create a page on your wiki called <code>Template:Box</code>:
 
  +
{{{1}}}
 
</div></pre>
   
* Add the following code to that page:
+
* Save the page and note that it displays the following content:
   
 
:<div style="width:10em; height:5em; border:1px solid #999; background-color:#CDF; color:#000; text-align:center; padding-top:2.5em;">{{{1}}}</div>
<pre>
 
<div style="width:10em; height:5em; border:1px solid #333; background-color:#666; color:#fff; text-align:center; padding-top:2em;">{{{1}}}</div>
 
</pre>
 
 
* Save the page and note that it displays the following content:
 
 
:<div style="width:10em; height:5em; border:1px solid #333; background-color:#666; color:#fff; text-align:center; padding-top:2.5em;">{{{1}}}</div>
 
 
* Create an article page on your wiki called <code>Template Testing</code>
 
   
 
* 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="width:10em; height:5em; border:1px solid #333; background-color:#666; color:#fff; text-align:center; padding-top:2.5em;">example text</div>
+
:<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.
   
  +
The number represents the number of the parameter:
===='''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>, ad infinitum. 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 | must still be included. For example:
+
If you wish not to use a certain parameter, you can leave it blank, but the | must still be included. For example:
 
 
:<code><nowiki>{{box|first parameter||third parameter}}</nowiki></code>
 
:<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.
   
 
* Using the same example pages as before, edit "Template:Box" and replace the content with the following code:
===''Example of 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.
 
 
* Using the same example pages as above, edit Template:Box and replace the content with the following code:
 
   
 
<pre>
 
<pre>
<div style="width:10em; height:5em; border:1px solid #333; background-color:{{{bgcolor|#666}}}; color:{{{textcolor|#fff}}}; text-align:center; padding-top:2em;">{{{text}}}</div>
+
<div style="width:10em; height:5em; border:1px solid #999; background-color:{{{bgcolor|#CDF}}}; color:{{{textcolor|#000}}}; text-align:center; padding-top:2em;">
  +
{{{text}}}
  +
</div>
 
</pre>
 
</pre>
   
: <code><nowiki>{{{text}}}</nowiki></code> introduces the concept of a '''named parameter'''. <code><nowiki>{{{bgcolor|#666}}}</nowiki></code> also introduces the concept of a '''default parameter''': if bgcolor is not defined, #666 will be passed.
+
: <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 passed.
   
* Edit the page "Template Testing" 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="width:10em; height:5em; border:1px solid #333; background-color:navy; color:white; text-align:center; padding-top:2em;">A navy blue box</div>
+
:<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'''====
+
====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="width:10em; height:5em; border:1px solid #333; background-color:#666; color:white; text-align:center; padding-top:2em;">A navy blue box</div>
+
:<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>
  +
{{box
  +
|bgcolor = navy
  +
|textcolor = white
  +
|text = A navy blue box
  +
}}
  +
</pre>
   
 
==See also==
 
==See also==
* {{#NewWindowLink:wikipedia:Help:Template#Parameters|Parameter help on Wikipedia}}
+
* {{#NewWindowLink:wikipedia:Help:Template#Parameters|More detailed Parameter help on Wikipedia}}
 
* [[Help:Templates|Templates]]
 
* [[Help:Templates|Templates]]
 
* [[Help:Parser functions|Parser functions]]
 
* [[Help:Parser functions|Parser functions]]

Revision as of 19:45, 21 March 2014

Templates allow you to pass parameters that can be used to affect the content or design of the template.

Simple parameters

  • Create a page on your wikia called "Template:Box":
  • Add the following code to that page:
<div style="width:10em; height:5em; border:1px solid #999; background-color:#CDF; color:#000; text-align:center; padding-top:2em;">
{{{1}}}
</div>
  • Save the page and note that it displays the following content:
{{{1}}}
  • Create an article page on your wikia 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 | must still be included. For example:

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

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.

  • Using the same example pages as before, edit "Template:Box" and replace the content with the following code:
<div style="width:10em; height:5em; border:1px solid #999; background-color:{{{bgcolor|#CDF}}}; color:{{{textcolor|#000}}}; text-align:center; padding-top:2em;">
{{{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 passed.
  • 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 to aid readability. It is not unusual to see them written in this form:

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

See also