Difference between revisions of "Template Test"

From ShieldKings Wiki
Jump to: navigation, search
(Created page with "Test Template '''A little thank you...''' for {{{1}}}. hugs, {{{2}}} <noinclude> {{PAGENAME}}</noinclude>")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
Test Template
+
Test Template from [http://www.mediawiki.org/wiki/Help:Templates wiki help]
  
'''A little thank you...'''
+
== Parameters == <!--T:26-->
 +
 
 +
<!--T:27-->
 +
To enrich the mechanism of transclusion, MediaWiki allows parameters to be passed to a template when it is transcluded. Parameters allow the template to produce different contents or have different behaviors.
 +
 
 +
<!--T:28-->
 +
Suppose you wish to insert a little thank you note in the [[Special:MyLanguage/Help:Talk pages|talk page]] of other users, such as:
 +
 
 +
<!--T:29-->
 +
{{Thankyou|all your effort|Me}}
 +
 
 +
<!--T:30-->
 +
The thank you note will have a reason (in this case, "all your effort") and a signature ("Me"). Your objective is that any user is able to thank any other user, for any reason whatsoever.
 +
 
 +
<!--T:31-->
 +
So that the note will look similar everywhere it is used, you can define a template called [[Template:Thankyou]], for example. Although the note should look similar whenever a user thanks another user, its specific contents (i.e. the reason and the signature) will be different. For that reason, you should pass them as parameters. If we ignore the remaining elements to format the box and place the image, the core content of the template will be this:
 +
 
 +
<!--T:32-->
 +
<pre><nowiki>'''A little thank you...'''
 
for {{{1}}}.
 
for {{{1}}}.
hugs, {{{2}}}
+
hugs, {{{2}}}</nowiki></pre>
 +
 
 +
<!--T:33-->
 +
Notice the use of <tvar|1><code><nowiki>{{{1}}}</nowiki></code></> and <tvar|2><code><nowiki>{{{2}}}</nowiki></code></>. This is the way to identify, within templates, the parameters that will be passed in when the template is used. Note that, within the template, each parameter is surrounded by '''three''' braces: <tvar|braces><code><nowiki>{{{ }}}</nowiki></code></>. This is different from normal template name usage.
 +
 
 +
<!--T:34-->
 +
When using the template on a page, you fill in the parameter values, separated by a "pipe" character (<tvar|pipe><code>|</code></>). MediaWiki allows parameters to be passed to the template in three ways: Anonymously, Numbered, and Named.
 +
 
 +
=== Anonymous parameters === <!--T:35-->
 +
 
 +
<!--T:36-->
 +
To pass in anonymous parameters, list the values of those parameters sequentially:
 +
 
 +
<!--T:37-->
 +
<nowiki>{{Thankyou|all your effort|Me}}</nowiki>
 +
 
 +
<!--T:38-->
 +
In this case, the <code><nowiki>{{Thankyou}}</nowiki></code> template receives parameters <code><nowiki>{{{1}}}=all your effort</nowiki></code> and <code><nowiki>{{{2}}}=Me</nowiki></code>, producing:
 +
 
 +
<!--T:39-->
 +
{{Thankyou|all your effort|Me}}
 +
 
 +
<!--T:40-->
 +
The order in which anonymous parameters are passed in is crucial to its behavior. Reversing the order of the parameters, like so:
 +
 
 +
<!--T:41-->
 +
<nowiki>{{Thankyou|Me|all your effort}}</nowiki>
 +
 
 +
<!--T:42-->
 +
would produce this result:
 +
 
 +
<!--T:43-->
 +
{{Thankyou|Me|all your effort}}
 +
 
 +
<!--T:44-->
 +
'''Note:''' identifying parameters by order (with <code><nowiki>{{{1}}}</nowiki></code>, etc) works ''only'' with anonymous parameters. If your page identifies any parameter by number or name, as shown below, this method will no longer be available to the template which receives them.
 +
 
 +
=== Numbered parameters === <!--T:45-->
 +
 
 +
<!--T:46-->
 +
To pass in parameters by number, identify each parameter when passing it:
 +
 
 +
<!--T:47-->
 +
<nowiki>{{Thankyou|2=Me|1=your friendship}}</nowiki>
 +
 
 +
<!--T:48-->
 +
This time, template <code><nowiki>{{Thankyou}}</nowiki></code> receives parameters <code><nowiki>{{{1}}}=your friendship</nowiki></code> and <code><nowiki>{{{2}}}=Me</nowiki></code>, though they have been supplied in inverse order, and produces:
 +
{{Thankyou|2=Me|1=your friendship}}
 +
 
 +
=== Named parameters === <!--T:49-->
 +
 
 +
<!--T:50-->
 +
The third way of passing parameters is by name, instead of numbers. In this case, the template contents would be changed to:
 +
 
 +
<!--T:51-->
 +
<pre><nowiki>'''A little thank you...'''
 +
for {{{reason}}}.
 +
hugs, {{{signature}}}</nowiki></pre>
 +
 
 +
<!--T:52-->
 +
Within the template, we use <code><nowiki>{{{reason}}}</nowiki></code> and <code><nowiki>{{{signature}}}</nowiki></code> to identify each parameter, instead of a number. To pass these parameters by name, identify each parameter when passing it:
 +
 
 +
<!--T:53-->
 +
<nowiki>{{Thankyou|signature=Me|reason=being who you are}}</nowiki>
 +
 
 +
<!--T:54-->
 +
In this case, template <code><nowiki>{{Thankyou}}</nowiki></code> receives parameters <code><nowiki>{{{reason}}}=being who you are</nowiki></code> and <code><nowiki>{{{signature}}}=Me</nowiki></code> and produces:
 +
 
 +
<!--T:55-->
 +
{{Thankyou|signature=Me|reason=being who you are}}
 +
The advantage of using named parameters in your template, besides also being flexible in the order parameters can be passed, is that it makes the template code much easier to understand if there are many parameters.
 +
 
 +
=== Default values === <!--T:56-->
 +
 
 +
<!--T:57-->
 +
If you transclude a template that expects parameters, but do not provide them, in this way:
 +
 
 +
<!--T:58-->
 +
<nowiki>{{Thankyou}}</nowiki>
 +
 
 +
<!--T:59-->
 +
in the numbered parameters example above you would get the following:
 +
 
 +
<!--T:60-->
 +
{{Thankyou}}
 +
 
 +
<!--T:61-->
 +
Since no parameters were passed in, the template presents the parameters themselves, instead of their respective values. In these cases, it may be useful to define ''default'' values for the parameters, i.e. values that will be used if no value is passed in. For example, if the template contents are changed to:
 +
 
 +
<!--T:62-->
 +
<pre><nowiki>'''A little thank you...'''
 +
for {{{reason|everything}}}.
 +
hugs, {{{signature|Me}}}</nowiki></pre>
 +
 
 +
<!--T:63-->
 +
then <code><nowiki>{{{reason|everything}}}</nowiki></code> defines that if no parameter <code>{{{reason}}}</code> is provided, then the value <code>everything</code> will be used. Similarly, <code><nowiki>{{{signature|Me}}}</nowiki></code>, defaults parameter <code>{{{signature}}}</code> to value <code>Me</code>. Now, transcluding the template again without passing any parameter, results in the following:
 +
</translate>
 +
<!-- strictly speaking, this example is false, because the template call was changed to obtain the desired effect - but the effect is the same if the template is defined as described -->
 +
<translate>
 +
<!--T:64-->
 +
{{Thankyou|reason=everything|signature=Me}}
 +
 
 +
 
 +
== Template Used==
 +
[[Template:Thankyou]]
 +
 
 +
<pre><div class="noprint" style="float:none; border:1px solid blue;width:200px;background-color:#F5F5F5;padding:2px;">
 +
{| cellspacing="0"
 +
| [[Image:Example.jpg|none|80px|Example sunflower image]]
 +
| style="padding-left:5px;"| '''A little thank you...''' <br /><small>for {{{reason|{{{1}}}}}}. <br />hugs, {{{signature|{{{2}}}}}}</small>
 +
|}</div></pre>
 +
 
  
 
<noinclude>
 
<noinclude>
 
[[Category:Template examples|{{PAGENAME}}]]</noinclude>
 
[[Category:Template examples|{{PAGENAME}}]]</noinclude>

Latest revision as of 17:31, 8 March 2014

Test Template from wiki help

Parameters

To enrich the mechanism of transclusion, MediaWiki allows parameters to be passed to a template when it is transcluded. Parameters allow the template to produce different contents or have different behaviors.

Suppose you wish to insert a little thank you note in the talk page of other users, such as:

Example sunflower image
A little thank you...
for all your effort.
hugs, Me


The thank you note will have a reason (in this case, "all your effort") and a signature ("Me"). Your objective is that any user is able to thank any other user, for any reason whatsoever.

So that the note will look similar everywhere it is used, you can define a template called Template:Thankyou, for example. Although the note should look similar whenever a user thanks another user, its specific contents (i.e. the reason and the signature) will be different. For that reason, you should pass them as parameters. If we ignore the remaining elements to format the box and place the image, the core content of the template will be this:

'''A little thank you...'''
for {{{1}}}.
hugs, {{{2}}}

Notice the use of <tvar|1>{{{1}}}</> and <tvar|2>{{{2}}}</>. This is the way to identify, within templates, the parameters that will be passed in when the template is used. Note that, within the template, each parameter is surrounded by three braces: <tvar|braces>{{{ }}}</>. This is different from normal template name usage.

When using the template on a page, you fill in the parameter values, separated by a "pipe" character (<tvar|pipe>|</>). MediaWiki allows parameters to be passed to the template in three ways: Anonymously, Numbered, and Named.

Anonymous parameters

To pass in anonymous parameters, list the values of those parameters sequentially:

{{Thankyou|all your effort|Me}}

In this case, the {{Thankyou}} template receives parameters {{{1}}}=all your effort and {{{2}}}=Me, producing:

Example sunflower image
A little thank you...
for all your effort.
hugs, Me


The order in which anonymous parameters are passed in is crucial to its behavior. Reversing the order of the parameters, like so:

{{Thankyou|Me|all your effort}}

would produce this result:

Example sunflower image
A little thank you...
for Me.
hugs, all your effort


Note: identifying parameters by order (with {{{1}}}, etc) works only with anonymous parameters. If your page identifies any parameter by number or name, as shown below, this method will no longer be available to the template which receives them.

Numbered parameters

To pass in parameters by number, identify each parameter when passing it:

{{Thankyou|2=Me|1=your friendship}}

This time, template {{Thankyou}} receives parameters {{{1}}}=your friendship and {{{2}}}=Me, though they have been supplied in inverse order, and produces:

Example sunflower image
A little thank you...
for your friendship.
hugs, Me


Named parameters

The third way of passing parameters is by name, instead of numbers. In this case, the template contents would be changed to:

'''A little thank you...'''
for {{{reason}}}.
hugs, {{{signature}}}

Within the template, we use {{{reason}}} and {{{signature}}} to identify each parameter, instead of a number. To pass these parameters by name, identify each parameter when passing it:

{{Thankyou|signature=Me|reason=being who you are}}

In this case, template {{Thankyou}} receives parameters {{{reason}}}=being who you are and {{{signature}}}=Me and produces:

Example sunflower image
A little thank you...
for being who you are.
hugs, Me

The advantage of using named parameters in your template, besides also being flexible in the order parameters can be passed, is that it makes the template code much easier to understand if there are many parameters.

Default values

If you transclude a template that expects parameters, but do not provide them, in this way:

{{Thankyou}}

in the numbered parameters example above you would get the following:

Example sunflower image
A little thank you...
for {{{1}}}.
hugs, {{{2}}}


Since no parameters were passed in, the template presents the parameters themselves, instead of their respective values. In these cases, it may be useful to define default values for the parameters, i.e. values that will be used if no value is passed in. For example, if the template contents are changed to:

'''A little thank you...'''
for {{{reason|everything}}}.
hugs, {{{signature|Me}}}

then {{{reason|everything}}} defines that if no parameter {{{reason}}} is provided, then the value everything will be used. Similarly, {{{signature|Me}}}, defaults parameter {{{signature}}} to value Me. Now, transcluding the template again without passing any parameter, results in the following: </translate> <translate>

Example sunflower image
A little thank you...
for everything.
hugs, Me


Template Used

Template:Thankyou

<div class="noprint" style="float:none; border:1px solid blue;width:200px;background-color:#F5F5F5;padding:2px;">
{| cellspacing="0"
| [[Image:Example.jpg|none|80px|Example sunflower image]]
| style="padding-left:5px;"| '''A little thank you...''' <br /><small>for {{{reason|{{{1}}}}}}. <br />hugs, {{{signature|{{{2}}}}}}</small>
|}</div>