Actions

Help

Difference between revisions of "Template"

From Retro CDN

(Created page with "{{outdated}} '''← Help: Contents''' The MediaWiki software allows you to automatically substitute '''messages and templates''' in the article text. As of the upgrad...")
 
 
Line 1: Line 1:
{{outdated}}
 
 
'''← [[Help: Contents]]'''
 
'''← [[Help: Contents]]'''
  
The MediaWiki software allows you to automatically substitute '''messages and templates''' in the article text.  As of the upgrade to MediaWiki 1.3 (in September 2004), templates now have their own [[Help:namespace|namespace]] inside the database. Template pages will start with the ''Template:'' prefix.
+
A key component of the Mediawiki software is the concept of '''templates''' - small sections of wikicode that are used to represent larger sections. The Retro wikis use templates extensively to minimise the amount of typing needed to produce a page. It also allows pages to share code, reducing the amount of wiki maintenance.
  
==Include a message==
+
Template pages are housed exclusively in the template namespace, and so start must start with the prefix "Template:".
The syntax for insertion of the page "Template:name" is <nowiki>{{name}}</nowiki>.  This reference in the wikitext causes insertion of the template content when the referring page is rendered.
 
  
If the page "Template:name" does not exist, then <nowiki>{{name}}</nowiki> works as <nowiki>[[Template:name]]</nowiki>, a link to a non-existing page, leading to the edit page.
+
==Basic use==
 +
If you're familiar with any form of computer programming, a Mediawiki template can be likened to a function call. A template has a '''name''' and an optional list of '''parameters''' (arguments). When used on a wiki page, templates usually take the form of:
  
Self links do not give any complication: they are neatly displayed in bold type and not as links. However, templates should have the correct page names and not rely on redirecting, since the feature does not work in that case.
+
:<pre>{{test|value1|value2}}</pre>
  
==Parameters==
 
You may also include a message as a '''template''' by applying parameters to the template.
 
  
Template parameters are either named or numbered. The syntax to add a message with parameters is <tt><nowiki>{{thing|param=value}}</nowiki></tt> with <tt><nowiki>{{{param}}}</nowiki></tt> tags in the template, or <tt><nowiki>{{thing|value1|value2}}</nowiki></tt> with <tt><nowiki>{{{1}}}</nowiki></tt>, <tt><nowiki>{{{2}}}</nowiki></tt>, etc. tags in the template. (Note that parameter tags in the template have ''three'' braces rather than two).
+
Arguments (in this case, "value1" and "value2") are passed to the template when the page is saved. For all intents and purposes, there is no limit to the amount of arguments that can be passed. Templates are identified by the use of double curly brackets (opening with "<nowiki>{{</nowiki>" and closing with "<nowiki>}}</nowiki>"), with arguments separated with the "|" (pipe) character.
  
On rendering, surplus parameters are ignored. In the case of too few, the parameters will appear as links to nonexistent templates.
+
Note: there are some exceptions to this rule - a "#" symbol after the opening curly brackets signals an internal function is being called (e.g. the conditional statement, "<nowiki>{{#if:</nowiki>"). This will be covered in a different help page.
  
Parameter templates are used in the template <nowiki>{{cleanup-reason}}</nowiki>, for example. The template call
+
The above example is attempting to call "Template:test" and pass it two arguments; "value1" and "value2".
  
:<nowiki>{{cleanup-reason|Make this more [[Michael Jackson|awesome!]]}}</nowiki>
+
Here is what Template:test might look like:
  
will produce the following note:
+
:<pre>Hello {{{1}}}. Learning how to use templates is {{{2}}}.</pre>
  
{{cleanup-reason|1=Make this more  [[Michael Jackson|awesome!]]|2=nocat}}
 
  
This example shows that you can even use wiki links in template parameters. This is not necessary if you are adding a well-formed URL - those are presented as links automatically.
+
When the page is saved, Template:test is coped in its entirity and placed where the template exists on the page. <nowiki>{{{1}}}</nowiki> and <nowiki>{{{2}}}</nowiki> will be replaced with the two arguments.
  
==Substitutions==
+
If done correctly,
If you include the string "subst:" after the double opening curly-braces, MediaWiki will ''substitute'' the template text in the wikitext rather than include it.  The net effect is that the current text of the template will be permanently saved, and will not be updated if and when the template is updated.
 
  
'''Example:''' <nowiki>{{subst:copyvio1}}</nowiki>
+
:<pre>{{test|Muhammad Ali|fun}}</pre>
  
When subst is applied to a template, a variable in the template is copied as such in the wikitext.
 
  
You cannot substitute the same template more than five times per edit.  (However, this only applies to any one edit; you can then go back and add the same template five more times.  Why anyone might one to do this, though, is unclear.)
+
should produce
  
==Usage==
+
:<pre>Hello Muhammad Ali. Learning how to use templates is fun.</pre>
Templates are useful for any text for which one wants a copy in two or more pages, and there is no need for each copy to be edited independently, to adapt it to the page it is in. Since parameters can be used, versions may to that extent even be different, and parameter values can be edited independently for each.
 
  
'''See also:''' [[:Category:Sonic Retro Templates]]
+
 
 +
In this example, the arguments were not given names, so are represented by numbers. If we wanted to be a little cleaner, the template might look something like this:
 +
 
 +
:<pre>Hello {{{name}}}. Learning how to use templates is {{{adjective}}}.</pre>
 +
 
 +
 
 +
To get the same result, our call must be changed too:
 +
 
 +
:<pre>{{test|name=Muhammad Ali|adjective=fun}}</pre>
 +
 
 +
 
 +
Templates can get very complicated, however, so it is often wise to use a different format to make it more readible:
 +
 
 +
<pre>{{test
 +
| name=Muhammad Ali
 +
| adjective=fun
 +
}}</pre>
 +
 
 +
 
 +
While the difference is negligible in a template as small as this, massive templates maintained by dozens of people rely on good formatting, so it's good to get into the habit early on.
 +
 
 +
==Practical examples==
 +
[[Template:Color]] exists to quickly change the colour of text. This saves editors having to write long-handed HTML code every time they want to change the colour.
 +
 
 +
e.g.:
 +
 
 +
<pre>{{color|color=blue|background=#FF7777|content=bloogly doogle}}</pre>
 +
 
 +
 
 +
produces
 +
 
 +
{{color|color=blue|background=#FF7777|content=bloogly doogle}}
 +
 
 +
 
 +
Retro CDN mainly uses templates to mark files - in [[:File:Sonic Adventure title.png]], the template <nowiki>{{DCSS}}</nowiki> is used to both categorise the Sonic Adventure title screen as a Dreamcast screenshot, but also to add the message describing the file as such. On Sega Retro, Template:DCSS is used for every Dreamcast screenshot (of which there are hundreds) - if our legal ramblings need to change, we only need to edit one template, as opposed to 100+ individual files.
 +
 
 +
Templates can also be "nested". DCSS calls [[Template:Imagetag]]. Templates can also be passed as arguments.
  
 
[[Category:Help]]
 
[[Category:Help]]

Latest revision as of 09:23, 31 March 2018

Help: Contents

A key component of the Mediawiki software is the concept of templates - small sections of wikicode that are used to represent larger sections. The Retro wikis use templates extensively to minimise the amount of typing needed to produce a page. It also allows pages to share code, reducing the amount of wiki maintenance.

Template pages are housed exclusively in the template namespace, and so start must start with the prefix "Template:".

Basic use

If you're familiar with any form of computer programming, a Mediawiki template can be likened to a function call. A template has a name and an optional list of parameters (arguments). When used on a wiki page, templates usually take the form of:

{{test|value1|value2}}


Arguments (in this case, "value1" and "value2") are passed to the template when the page is saved. For all intents and purposes, there is no limit to the amount of arguments that can be passed. Templates are identified by the use of double curly brackets (opening with "{{" and closing with "}}"), with arguments separated with the "|" (pipe) character.

Note: there are some exceptions to this rule - a "#" symbol after the opening curly brackets signals an internal function is being called (e.g. the conditional statement, "{{#if:"). This will be covered in a different help page.

The above example is attempting to call "Template:test" and pass it two arguments; "value1" and "value2".

Here is what Template:test might look like:

Hello {{{1}}}. Learning how to use templates is {{{2}}}.


When the page is saved, Template:test is coped in its entirity and placed where the template exists on the page. {{{1}}} and {{{2}}} will be replaced with the two arguments.

If done correctly,

{{test|Muhammad Ali|fun}}


should produce

Hello Muhammad Ali. Learning how to use templates is fun.


In this example, the arguments were not given names, so are represented by numbers. If we wanted to be a little cleaner, the template might look something like this:

Hello {{{name}}}. Learning how to use templates is {{{adjective}}}.


To get the same result, our call must be changed too:

{{test|name=Muhammad Ali|adjective=fun}}


Templates can get very complicated, however, so it is often wise to use a different format to make it more readible:

{{test
| name=Muhammad Ali
| adjective=fun
}}


While the difference is negligible in a template as small as this, massive templates maintained by dozens of people rely on good formatting, so it's good to get into the habit early on.

Practical examples

Template:Color exists to quickly change the colour of text. This saves editors having to write long-handed HTML code every time they want to change the colour.

e.g.:

{{color|color=blue|background=#FF7777|content=bloogly doogle}}


produces

bloogly doogle


Retro CDN mainly uses templates to mark files - in File:Sonic Adventure title.png, the template {{DCSS}} is used to both categorise the Sonic Adventure title screen as a Dreamcast screenshot, but also to add the message describing the file as such. On Sega Retro, Template:DCSS is used for every Dreamcast screenshot (of which there are hundreds) - if our legal ramblings need to change, we only need to edit one template, as opposed to 100+ individual files.

Templates can also be "nested". DCSS calls Template:Imagetag. Templates can also be passed as arguments.