Help:Templates/Parameters

From The Nitrome Wiki
Jump to navigation Jump to search

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

When inserting templates on pages, the visual editor allows you to define and insert parameter text without using wikitext.

Simple parameters[edit source]

Example[edit source]

A user creates a template titled "Template:Box", creating the page with the following code in source mode:

<div style="width:10em; height:5em; border:1px solid #999; background-color:#CDF; color:#000; text-align:center; padding-top:2em;">
{{{1}}}
</div>

Upon saving the page, the page displays the following:

{{{1}}}

The user then goes to any article on the wiki, clicks "Edit", and enters the following code in source mode:

{{Box|Example text}}

The user then saves the page, the page displaying the following:

Example text

Understanding what happened[edit source]

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[edit source]

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 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[edit source]

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
}}


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.

See also[edit source]

  • {{#NewWindowLink:wikipedia:Help:Template#Parameters|More detailed Parameter help on Wikipedia}}
  • Templates
  • Help:Infoboxes and Rappy 4187's guide to infoboxes
  • Parser functions