Help:Templates: Difference between revisions

(A bit of rewording)
(Rewrote)
 
Line 1: Line 1:
{{help}}
{{help}}


'''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.


'''Templates''' are used on Grand Theft Wiki to insert the contents of a template into a page.
If on a page you add the code <code><nowiki>{{name}}</nowiki></code>, 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.


This can be used for messages like {{template|help}}, navigation boxes like {{template|gta}}, [[Help:Userboxes|Userboxes]], or more complicated templates that take input, such as {{template|template}}.
For example, the code <code><nowiki>{{help}}</nowiki></code> transcludes [[Template:Help]], which is the white+red message box at the very top of this page.
{{toc}}


<nowiki>Templates are all stored at [[Template:Templatename]]. They can then be included into a page using the code {{templatename}}.</nowiki>
==What are Templates used for?==
Templates are used for many different things:
*[[Help:Message Boxes|Message Boxes]] that serve notices at the top of a page. You can see these all over the wiki from {{template|cleanup}} to {{template|admin}}
*[[Help:Infoboxes|Infoboxes]] that display a table of useful information at the top of an article
*[[Help:Userboxes|Userboxes]] that users use on their pages to show who they are and what they like
*Navigation templates like {{template|gta}} which are shown at the bottom of articles
*Other templates are used to perform a specific function in code, such as with [[Template:Col-begin|columns]] or [[Template:Template]]


<nowiki>Whatever is on the [[Template:Templatename]] 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.</nowiki>


<nowiki>Templates that require input are used like this: {{templatename | Option 1 | Option 2}}. In the code of the template, those options are referenced as {{{1}}} and {{{2}}} etc. There are also options with specific names, such as {{templatename | title=Hello}}, which is referenced in the template code as {{{title}}}.</nowiki>
==Using Templates==


<nowiki>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.</nowiki>


For example:


For example, {{template|help}} shows up the banner at the top of this page. It is defined at [[Template:help]]. That template shows the banner and also assigns any page it is used on to a [[Help:categories|category]], which is [[:Category:Help]]. Not all templates do this.
<pre>{{spoiler}}</pre>


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


Whenever the template is edited, each page that uses that template will change too. If you just want the template contents ''copied'' into the page rather than actively linked, you can use <nowiki>{{subst templatename}}</nowiki>. Subst simply includes the contents of templatename once.
{{spoiler}}


===Inputs & Options===


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


Any page can be used as a template, by putting a colon (:) at the start of the tag. <nowiki>{{:Help:Templates}}</nowiki> would show the contents of the [[Help:Templates]] page.  
For example, {{template|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:
 
<pre>{{info | This is a message }}</pre>
This produces:
{{info | This is a message }}
 
Some templates let you specify many parameters in a certain order, so you might have <code><nowiki>{{template|input 1|input 2|input 3|input 4}}</nowiki></code>
 
Other templates give you a choice of parameters you specify by name. So to set the "title" and "width" parameters, you would do this:
 
<pre>
{{template|title=This is a template|width=200px}}
</pre>
 
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 <code><nowiki>{{:Help:Templates}}</nowiki></code> 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 <nowiki>{{/infobox}}</nowiki>.
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 <nowiki>{{/infobox}}</nowiki>.




*'''[http://www.grandtheftwiki.com/index.php?title=Special%3AAllpages&from=&namespace=10 View all templates]'''
===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:
<pre>
{{subst:template}}
{{subst:template|input1|input2}}
</pre>
 
 
==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:
<pre>{{template | Hello}}</pre>
 
Inside the template code you use <code><nowiki>{{{1}}}</nowiki></code>. 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 <code><nowiki>{{{2}}}  {{{3}}}  {{{4}}}</nowiki></code> etc
 
;Named Parameters
 
In a similar fashion, named parameters can be specified.
 
<pre>{{template|title=This is a Template|width=200px}}</pre>
 
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:
 
<pre>
<div style="width: {{{width}}}px;">{{title}}</div>
</pre>
 
;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:
 
<pre>{{{1|DefaultText}}}</pre>
 
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)
 
 
*'''[[Special:Allpages/Template:|View all templates]]'''
*[[mw:Help:Templates|Help:Templates at MediaWiki]]
*[[mw:Help:Templates|Help:Templates at MediaWiki]]

Latest revision as of 17:49, 26 April 2012


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)