Help:Templates

From Festipedia, hosted by the FR Heritage Group
Jump to navigation Jump to search

Templates are pages which can be inserted into other pages. When you view the page, the reference to a template is replaced by the content of the template.

Templates are useful if the same text needs to appear on several pages. Use of a template means that all the pages on which the text appears can be updated with a single change. Templates can also be used to add frequently used messages to pages.

Another use is for consistent formatting of information. For example, a template could be set up with a standard layout for the basic information on locomotives (typically an infobox. Any article about a locomotive could then use this template with appropriate parameters to give the relevant information for that locomotive.

The template {{stub}} is used to make incomplete articles (stubs) accessible in a category of their own.

General

Templates are held in a special namespace called, not surprisingly, Template:.

To insert a template in a page, simply type {{template name}}. If you do not specify a namespace, the template will be taken from the Template namespace. You can use a page from another namespace as a template simply by specifying the namespace, e.g. {{Help:Example}} would use the Example page from the Help namespace. If you want to use a page from the main namespace, simply put a colon before the pagename, e.g. {{:Example}}. However, pages outside the Template namespace should only be used in this way in exceptional circumstances.

If you include a reference to an anchor when calling a template, e.g. {{Example#anchor}}, the anchor will be ignored.

You can link to a template page in the normal way, e.g. [[Template:Example]].

Using a template which does not exist is equivalent to linking to a non-existent page, so {{Example}} will give a link to the edit page for Template:Example. This means you can set up a template by including the template tag then following the link to create the page.

Template names are case sensitive apart from the first letter, just like page names.

As a simple example, typing abc{{in}}def where Template:In contains "Fred" would give "abcFreddef". Note that the new line at the end of the template is automatically removed when the template is inserted.

If the template starts with one of the list characters (*#;:) it will be treated as starting on a new line. If you don't want that, put <nowiki></nowiki> tags around the character.

If a template is a redirect page, the target page will be used. For example, if you put {{Template A}} on a page and Template A is redirected to Template B, the result will be the same as if you had put {{Template B}}.

A template can itself invoke another template. However, it cannot invoke itself, either directly or indirectly. This is known as recursion and is not allowed. There may be some circumstances where recursion is useful so a form of recursion using redirects is allowed. If Template A uses Template B and Template B is a redirect to Template A, that will be allowed. If Template A then uses Template B again, that will fail. However, if Template A now uses Template C which also redirects to Template A, that will work. So it is possible for a template to recursively call itself with the number of levels limited by the number of redirects.

Variables in the template are evaluated after it is inserted in the calling page. So, for example, {{PAGENAME}} will give the name of the page using the template, not the name of the template.

Templates are evaluated when the page is viewed. This means that the current version of the template is always used, even when you are viewing an old version of the page using the template.

Parameters

Templates containing boilerplate text are useful. However, templates can be made even more powerful through the use of parameters. These allow the template to change its behaviour based on information passed to it from the page that invokes it.

There are two approaches to parameters - they can be named or unnamed. The two approaches can be mixed together with care.

Named parameters

Named parameters are specified like this:

{{Example|first=Fred|second=Jim}}

This invokes Template:Example passing it parameters called "first" and "second" with values of "Fred" and "Jim" respectively. These parameters can be used in the template by placing the parameter name in three braces, for example:

{{{first}}}

You can specify a default value to be used if the parameter is not specified like this:

{{{first|default value}}}

If you are using named parameters, the order of the parameters does not matter.

Note that parameter names are case sensitive for ALL characters.

Putting all this together, imagine Template:LocoBuilt contains the text:

{{{Loconame}} was built in {{{Built}}}

On a page we type {{LocoBuilt}}. When that page is displayed, we will see "{{{Loconame}} was built in {{{Built}}}" because we have not specified any parameters for the template.

However, if we put {{LocoBuilt| Built=1893 | Loconame=Blanche }} we will get:

Blanche was built in 1893.

Unnamed parameters

If you do not specify names for the parameters, they are numbered, so:

{{Example|Fred|Jim}}

invokes Template:Example passing it parameters called "1" and "2" with values of "Fred" and "Jim" respectively. These parameters are used within the template in exactly the same way as named parameters, so:

{{{1|jkl}}}

will be replaced by the value of parameter 1 (Fred in this case) with a default of "jkl" if parameter 1 has not been specified.

For an example using unnamed parameters, let's set up Template:in containing the text:

start-{{{1|xyz}}}-end

If we put {{in|middle}} on a page, the result will be "start-middle-end".

Parameters in detail

When a template is called, parameters are evaluated left to right. If a parameter is declared more than once, the final declaration will be used, so:

{{Example|first=Fred|second=Jim|first=Bob}}

will result in the parameter "first" having the value "Bob". The initial assignment of "Fred" will be ignored.

You can use both named and unnamed parameters in one template. Any unnamed parameters are numbered starting from 1, so:

{{Example|first=Fred|second=Jim|hello|world}}

sets four parameters as follows:

  1. first, with a value of "Fred"
  2. second, with a value of "Jim"
  3. 1, with a value of "hello"
  4. 2, with a value of "world"

This applies even if we have already specifically named parameters using numbers, so:

{{Example|2=Fred|1=Jim|hello|world}}

passess only two parameters:

  1. 1, with a value of "hello"
  2. 2, with a value of "world"

For named parameters, whitespace before and after the parameter value is removed. However, if the parameter is unnamed, whitespace before and after the parameter value is not removed. Using Template:in (described above):

  • {{in|1= a}} gives "start-a-end"
  • {{in|1=a }} gives "start-a-end"
  • {{in| a}} gives "start- a-end"
  • {{in|a }} gives "start-a -end"

The default for a parameter is used only if the parameter is not declared when the template is invoked. A blank value will not cause the default to be used. So:

  • {{in|}} gives "start--end"
  • {{in}} gives "start-xyz-end"

A parameter value can include a piped internal link, so {{in|[[Blanche|b]]}} gives "start-b-end".

Any parameters passed to the template but not used in the template are simply ignored. {{in|a|b|c}} gives "start-a-end".

Numbering parameters explicitly allows you to specify them in an alternative order or even skip some completely.

{{Example|a|b|c}} is equivalent to {{Example|3=c|1=a|2=b}}

For an example of skipping a parameter, assume Template:Example contains start-{{{1|hello}}}-middle-{{{2|world}}}-end</tt>. Using it by typing {{Example|2=goodbye}}</tt> gives "start-hello-middle-goodbye-end".

You may want a template to behave differently depending on whether or not a parameter is present. You can do this simply using functions. For example, if your template has an optional parameter "Build date", you could use {{#if:{{{Build date}}}|Built {{{Build date}}}|}} to output the appropriate text depending on whether or not the parameter has been declared.

Parameters depending on parameters

You can make the value of a parameter depend on another parameter. For example, if you use {{ParExample|parameter=foo|foo=bar}} and Template:ParExample contains {{{{{{parameter}}}}}}, the result will be "bar".

Restrictions

  • If a parameter value contains an = character, you must declare it as a named parameter even when it is 1, 2, 3, 4, etc.
  • There is no restriction on the lengths of parameter names or values.
  • You can include template tags and parameters tags as within the parameters passed to a template.
  • If you need to include multiple opening or closing braces which are not part of a template or parameter tag, you will need to place them in <nowiki></nowiki> tags.
  • A parameter cannot contain pairs of opening or closing square brackets unless they are part of a link.

Templates and tables

In some circumstances, the fact that templates and tables both use the pipe (|) character can cause problems. You can usually get around this by using Template:! which contains a single pipe character.

Noinclude and includeonly

Any text within the template between <noinclude> and </noinclude> tags will not appear when you use the template. However, it will appear when you view the template. This is useful when you want to place a template in a category without placing pages that use it in the same category. You can also use this feature to document the template on the template page - the documentation will appear when you view the template but will be stripped out when you use the template.

The <includeonly> and </includeonly> tags have the opposite effect - text between these tags appears when the template is used in another page but does not appear when you view the template. This allows you to place pages using the template into a category without placing the template in the same category. It is also useful to strip out anything that works correctly when the template is used but looks messy when viewing the template.

msgnw

If you want to include the uninterpreted text of a template (i.e. the text you would see if you were editing the template), use the msgnw prefix. For example, if you wanted to include the raw text of Template:Example you would put {{msgnw:Example}}. Note that numbered and unnumbered lists within the template WILL be rendered even if the msgnw prefix is used.

subst

The subst prefix causes the template to be evaluated when the page is saved rather than when it is displayed, so {{subst:Example}} will be replaced by the text of Template:Example. If subst is used, anyone viewing the page will always see the template as it was at the time the page was saved. Subsequent changes to the template will not affect the page.

Special 'templates'

The wiki has a number of predefined 'templates' known as variables and functions. These do not have template pages as they are implemented in the software and cannot be changed.