Help:Templates

From Gentoo Wiki
Jump to: navigation, search

If you have standard texts you want to include on several pages, the MediaWiki template feature comes into play.

Creation

Templates are standard wiki pages whose content is designed to be transcluded (embedded) inside other pages. Templates follow a convention that the name is prefixed with "Template:", assigning it to that namespace; besides this, you can create them like any other wiki page.

The simplest use of templates is as follows. If you create a page called "Template:Welcome" with contents:

Hello! Welcome to the wiki.

you'll have created your first template! If you then insert the code:

{{Welcome}}

in any other page, when that page is viewed the text "Hello! Welcome to the wiki." will appear instead of {{Welcome}}. The template content is "transcluded" into the other page, i.e. it is integrated in the page.

You can then insert {{Welcome}} at any point of any page where you wish to welcome someone. Suppose it is used in 100 pages. If you then change the template contents to:

Hi there! Welcome to this wonderful wiki.

and revisit any of the 100 pages where the template was used, you'll see the new text instead of the original one. In this way, you have changed the content of 100 pages without editing them, because the template is transcluded into these pages.

This is the basic mechanism. There are several additional features of transclusion that enrich this mechanism and make templates very useful.

Usage

Templates can be used in other pages in these ways:

  • {{Name}}, described above, 'transcludes' (i.e. includes a copy of) the content of the template (stored in the page [[Template:Name]]) whenever the page containing the template transclusion is fetched and displayed; i.e. if the template is later changed, the displayed transcluding page will automatically change too
  • {{subst:Name}} replaces that string with the contents of the template, in the source of the transcluding page, when you save that page; the copy of the template contents can then be edited normally (and separately from the original in the template page). Note: don't use this if you are looking to continually propagate changes from the source template to the page(s) that references it.
  • {{safesubst:Name}} allows for substitution that doesn't break transclusion, see Help:Substitution on en.wikipedia.org.
  • {{msgnw:Name}} includes the template in a form that displays it as raw wiki syntax (the way <nowiki> does) when the page containing it is fetched.

In fact, an ordinary wiki page can also be used as a template, simply by specifying the namespace it resides in, so:

  • {{Template:Pagename}} includes [[Template:Pagename]]
  • {{Foo:Pagename}} includes [[Foo:Pagename]]
  • {{:Pagename}} includes [[Pagename]]
    • {{subst::Pagename}} replaces itself with the contents of [[Pagename]]

If no such namespace exists, the full title is assumed to be a template:

  • {{Foo:Bar}} includes [[Template:Foo:Bar]]

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 {{{1}}} and {{{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: {{{ }}}. 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 char (|). MediaWiki allows parameters to be passed to the template in three ways.

Anonymous parameters

To pass in anonymous parameters (also known as unnamed parameters), list the values of those parameters sequentially:

{{Thankyou|all your effort|Me}}

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

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

Inverting the order of the parameters:

{{Thankyou|Me|all your effort}}

causes template {{Thankyou}} to receive parameters {{{1}}}=Me and {{{2}}}=all your effort and inverts the result:

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

So, the order in which anonymous parameters are passed in is crucial to its behaviour.

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:

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

Very often inside templates you will see references to parameters in this form:

{{{A template parameter|}}}

Here the default value is an empty string, which will be used if no value is passed when calling the template.

Parameter values containing special characters

Because the characters = and | have special meanings when calling templates, particular care must be taken when passing a parameter value containg one or both of these symbols. The templates {{=}} and {{!}} (or the HTML entities &#61; and &#124;) can be used for this purpose.

To see how this works, consider the following mistakes a template user might make when trying to use special characters in template calls, and how they could be corrected. Each case begins with an indication of how the user wants the parameters to be parsed, followed by the user's unsuccessful template call (in which the parameters are actually parsed differently, as indicated in parentheses), and finally various successful template calls that would be parsed in the desired way. (The template being called is a hypothetical one named "Template".)

One anonymous parameter containing an equals sign
{{Template|foo=bar}} (parsed as a parameter named foo being assigned the value bar)
→ {{Template|foo{{=}}bar}}
→ {{Template|foo&#61;bar}}
→ {{Template|1=foo=bar}}
→ {{Template|<nowiki>foo=bar</nowiki>}}
One anonymous parameter containing a pipe symbol
{{Template|foo|bar}} (parsed as two anonymous parameters with values foo and bar)
→ {{Template|foo{{!}}bar}}
→ {{Template|foo&#124;bar}}
One named parameter with value containing a pipe symbol
{{Template|foo=bar|buzz}} (parsed as a parameter named foo being assigned the value bar, followed by an anonymous parameter with value buzz)
→ {{Template|foo=bar{{!}}buzz}}
→ {{Template|foo=bar&#124;buzz}}
One named parameter with value containing equals sign
{{Template|foo=bar=buzz}} (parsed as intended, as a parameter named foo being assigned the value bar=buzz)

Note that there is no such thing as a parameter name containing = or |, only parameter values. (Well, OK, technically parameter names can contain |, but you probably shouldn't worry about that.)

Controlling template inclusion

By default, the whole content of a template will be processed and displayed both when the template page is being viewed directly and when the template is being included in another page. However, you can control template inclusion by the use of <noinclude> and <includeonly> tags.

Anything between <noinclude> and </noinclude> will be processed and displayed only when the template's page is being viewed directly, and will not be processed and displayed when it is included in another page. The application of this is to do things to the template page that should not be similarly done to the pages where it is included:

  • Categorising the template.
  • Placing in the template interlanguage links to similar templates in other languages.
  • Presenting explanatory text about how to use the template.

Anything between <includeonly> and </includeonly> will be processed and displayed only when the page is being included, and will not be processed and displayed when the template page is being viewed directly. The application of this is to do things in the pages where the template is included that you do not want to be similarly done in the template page:

  • Categorising only the pages where the template is included. Note that when you change the categories applied by a template in this fashion, the categorization of all the pages where that template is included may not be updated until some time later: this is handled by the job queue. To force the re-categorisation of a particular page, open the page for edit and save it without changes.
  • Ensuring that the template's code is not executed when viewing the template page itself. Normally this is because it isn't receiving any parameters and its execution without parameters has an undesired aspect.

Of course, everything outside <noinclude> and <includeonly> tags is processed and displayed both when the template page is being viewed directly and when the template is being included in another page.

Adding documentation and categories

For templates to be effective, users need to find them, and find out how to use them.

To find templates users may:

  1. Click Special Pages > All Pages.
  2. In the Namespace list, choose Template and click Go.

But it is better if users can navigate a category structure to find appropriate templates for their task.

To provide users with this ability, and to give them helpful usage information, place the {{Documentation}} template at the bottom of any template you create:

<noinclude>
{{Documentation}}
</noinclude>

After saving, you will see a prominent "Documentation" section heading with a "[create]" link to the right of it. Following that link will create a documentation subpage preloaded with some default documentation. Modify the documentation as appropriate, describing your template and its usage — and don't forget to add one or more appropriate template categories near the bottom (inside of the <includeonly> element), so users can find your template.

Note
If nothing was preloaded when you initially came to the "/doc" subpage, you probably followed the red "/doc" link instead of the blue "[create]" link. You can try fixing the problem by saving the subpage with {{subst:Documentation/preload}} as its only content, which should create the subpage with the same content that would have been preloaded. Then you can edit the page as usual and start to modify the documentation. It's better to use the automatically preloaded version, though, since that will ensure that you are setting everything up correctly.

The {{Documentation}} template also provides links to create a template sandbox and a list of test cases. (These "create" links will be visible on the page for the template itself, not on the "/doc" subpage.)

Copying from one wiki to another

Templates often require CSS or other templates, so users frequently have trouble copying templates from one wiki to another. The steps below should work for most templates. (Make sure the wiki you are copying from has a license compatible with ours.)

MediaWiki code

If you have import rights on the new wiki:

  1. Go to Special:Export on the original wiki, and download an .xml file with the complete history of all necessary templates, as follows:
    • Enter the name of the template in the big text box.
    • Check the box "Include templates".
    • Uncheck the box "Include only the current revision".
    • Click Export.
  2. Go to Special:Import on the new wiki and upload the .xml file.

If you don't have import rights on the new wiki:

  1. Go to Special:Export on the original wiki, and download an .xml file with the latest version only of all necessary templates, as follows:
    • Enter the name of the template in the big text box.
    • Check the box "Include templates".
    • Check the box "Include only the current revision".
    • Click Export.
  2. Open the file in a text editor and manually copy the text inside the <text> tag of each listed template into a similarly named template in your wiki. In the edit summary of each template, link to the original page for attribution.

This will copy the entire code necessary, and will suffice for some templates.

Extensions

Check if the template depends on any extension. You have to convice sysops first to install the extension.

CSS and JavaScript code

Besides MediaWiki code, many templates make use of CSS and some rely on JavaScript to work fully. If the copied templates are not behaving as expected, this may be the cause. To copy the required CSS and JavaScript to your wiki you'll normally need to have admin priviledges, because you'll be editing system messages in the "MediaWiki:" namespace.

  1. Look for the use of CSS classes (text like class="foobar") in the template text. If those classes appear in "MediaWiki:Common.css" or "MediaWiki:Monobook.css" on the original wiki, copy them to "MediaWiki:Common.css" on the new wiki and check if the template is now fine.
  2. If the copied template is still not working as expected, check if there is code in "MediaWiki:Common.js" or "MediaWiki:Monobook.js" on the original wiki. If so, you can try copying it to "MediaWiki:Common.js" on the new wiki. Normally, it is a good idea to only copy code from trusted sources, and first browsing the code to identify and select the parts that seem relevant. You may find comments that can serve as clues to identify the functionality of each part. When in doubt, copy all the code to the new wiki.

See also