Templates allow you to pass parameters that can be used to affect the content or design of the template.
Step-by-Step
Example of Simple Replacement
- Create a page on your wiki called
Template:Box
:
- Add the following code to that page:
<div style="width:10em; height:5em; border:1px solid #333; background-color:#666; color:#fff; 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 wiki called
Template Testing
- 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}}}
, ad infinitum. 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}}
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:
<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>
{{{text}}}
introduces the concept of a named parameter.{{{bgcolor|#666}}}
also introduces the concept of a default parameter: if bgcolor is not defined, #666 will be passed.
- Edit the page "Template Testing" 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
See also
- {{#NewWindowLink:wikipedia:Help:Template#Parameters|Parameter help on Wikipedia}}
- Templates
- Parser functions