Help:Templates

From Grand Theft Wiki
Revision as of 17:49, 26 April 2012 by Gboyers (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Templates are used on Grand Theft Wiki to insert messages, boxes or code into other pages. Templates have a very wide range of uses on the wiki.

If on a page you add the code {{name}}, then when you preview or save, the contents of the page "Template:Name" will be shown wherever you add that code. That is called 'transcluding' a template.

For example, the code {{help}} transcludes Template:Help, which is the white+red message box at the very top of this page.


What are Templates used for?

Templates are used for many different things:

  • Message Boxes that serve notices at the top of a page. You can see these all over the wiki from {{cleanup}} to {{admin}}
  • Infoboxes that display a table of useful information at the top of an article
  • Userboxes that users use on their pages to show who they are and what they like
  • Navigation templates like {{gta}} which are shown at the bottom of articles
  • Other templates are used to perform a specific function in code, such as with columns or Template:Template


Using Templates

Whatever is on the [[Template:Name]] page will then show up in every instance of the {{Name}} tag on Grand Theft Wiki. If you change the template, every page using it will change too.

For example:

{{spoiler}}

This will then show the contents of Template:Spoiler, which produces:


Inputs & Options

Some templates have inputs and options, which are called 'parameters'.

For example, {{info}} has an input where you can specify the text to be shown in the template. This is simply done after a pipe character | like this:

{{info | This is a message }}

This produces:

Some templates let you specify many parameters in a certain order, so you might have {{template|input 1|input 2|input 3|input 4}}

Other templates give you a choice of parameters you specify by name. So to set the "title" and "width" parameters, you would do this:

{{template|title=This is a template|width=200px}}

Please note these are ONLY available where the template has been specifically made to use those parameters. You can't just specify the width= of a template that was not coded to accept that.


Using Other Pages

Sometimes we want to include content like a template, without having a whole template page

Any page in any namespace can be used as a template, by putting a colon (:) at the start of the tag. The code {{:Help:Templates}} would show the contents of the Help:Templates page.

Also, subpages can be used as templates (or links) directly with a slash (/) then the subpage name. Grand Theft Auto IV quickly includes it's subpage Grand Theft Auto IV/infobox with the tag {{/infobox}}.


Substitution

Using standard template code as above, the template code stays in the page, and the templates content is displayed whenever anyone views the page. Every time you change the template page, the content page changes.

In some situations, you want the code from the template to be completely inserted into the article instead. This then lets you edit the code within that article, and it's no longer 'connected' to the template's own page.

To do this, use subst: inside the template, for example:

{{subst:template}}
{{subst:template|input1|input2}}


Creating Templates

To create a template, just create a page starting with Template: such as Template:NewTemplate

Templates work just like regular pages. The code inside the template will be either displayed (transcluded) or copied (substituted) into the page, depending on which code is used above.


Parameters

To accept parameters, there are two ways to do this:

Ordered Parameters

Ordered parameters just accept the first, second, third (etc) input from the template's code

So for the template transcluding code like this:

{{template | Hello}}

Inside the template code you use {{{1}}}. Every time the template is viewed, that code will be replaced with the 1st parameter given (which is "Hello" above).

For further ordered parameters, you can use {{{2}}} {{{3}}} {{{4}}} etc

Named Parameters

In a similar fashion, named parameters can be specified.

{{template|title=This is a Template|width=200px}}

This transcludes Template:Template setting the parameters 'title' and 'width'

Inside the template's code, you can accept these parameters just like ordered parameters, but using the name instead of the number:

<div style="width: {{{width}}}px;">{{title}}</div>
Defaults

But what if someone doesn't specify the parameter you need? In the template's code, you can also set a default for a parameter like this:

{{{1|DefaultText}}}

This will be replaced with the 1st parameter if given, but will show 'DefaultText' if not. This can also work with numbers (such as a default width if one isn't specified)