m (Reverted edits by 74.105.121.132 (wall | block) to last version by Leviathan 89) |
(Update) |
||
Line 1: | Line 1: | ||
⚫ | |||
− | __NOTOC__ __NOEDITSECTION__ |
||
⚫ | |||
+ | ==Simple parameters== |
||
− | ==Step-by-Step== |
||
⚫ | |||
− | ===''Example of Simple Replacement''=== |
||
+ | * Add the following code to that page: |
||
⚫ | |||
⚫ | |||
+ | {{{1}}} |
||
⚫ | |||
− | * |
+ | * Save the page and note that it displays the following content: |
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
− | * Save the page and note that it displays the following content: |
||
− | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
* 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| |
+ | :<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 # |
+ | :<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> |
⚫ | |||
⚫ | |||
+ | 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: |
|
− | |||
:<code><nowiki>{{box|first parameter||third parameter}}</nowiki></code> |
:<code><nowiki>{{box|first parameter||third parameter}}</nowiki></code> |
||
+ | ==Named and default parameters=== |
||
⚫ | |||
⚫ | |||
− | ===''Example of Named and Default Parameters''=== |
||
− | |||
⚫ | |||
− | |||
⚫ | |||
<pre> |
<pre> |
||
− | <div style="width:10em; height:5em; border:1px solid # |
+ | <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|# |
+ | : <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 |
+ | * 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 # |
+ | :<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. |
|
− | + | 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 # |
+ | :<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: |
||
+ | |||
⚫ | |||
+ | {{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
- {{#NewWindowLink:wikipedia:Help:Template#Parameters|More detailed Parameter help on Wikipedia}}
- Templates
- Parser functions