Difference between revisions of "Help:Templates"

From Mario Fan Games Galaxy Wiki
(Creation)
 
Line 1: Line 1:
 
{{Help}}
 
{{Help}}
'''Templates''' are a modern convienience that are used widely across this wiki. Every template page has the prefix "Template:" so that you can easily identify them.
+
'''Templates''' are special pages existing within the "Template" namespace. These pages are designed explicitly for other pages to call them (or rather, "include"), meaning you can use one page as content in many pages.
  
==Usage==
+
For instance, this page calls [[Template:Help]], which may be edited to change the displayed help contents on all of the help pages that include it at once.
To call a template, you use the following
+
 
<pre>{{templatename}}</pre>
+
Templates are particularly useful for quickly embedding otherwise complex table code, while needing to give only the most vital information (see [[Template:MFGGer]].)
Templates can be as simple as this, but they can also be quite complicated, allowing numerous sections to be defined. It may look something like this
+
 
<pre>{{template|name=Fred|age=542|numberofcars=Lots}</pre>
+
== Inclusion ==
or it may have been organised so that it's easier on the eyes, like this
+
=== Basic templates ===
<pre>{{template
+
Templates are included in a page with their name ('''WITHOUT THE "TEMPLATE:" PART''') between point-curled brackets, as in <nowiki>{{-name-}}</nowiki>:
 +
<nowiki>{{Help}}</nowiki>
 +
This calls [[Template:Help]]. Except for the first letter ("H" in this case), all template names are case-sensitive. (e.g. <nowiki>{{Help}} and {{help}} are the same - but {{hElP}} is not)</nowiki>
 +
 
 +
=== Parameters ===
 +
The true power of templates is that they may be passed parameters when included, which they will utilize in some manner depending on the template itself. For instance, calling the (fictional) "Template:Car-salesman" with relevant information may look something like:
 +
<nowiki>{{car-salesman
 
|name=Fred
 
|name=Fred
|age=542
+
|age=46
|numberofcars=Lots
+
|car-count=Lots
}}</pre>
+
}}</nowiki>
Each section may automatically fill in part of a table, sentence... you name it. Often the areas won't be labelled to keep it simple
 
<pre>{{template|Fred|542|Lots}</pre>
 
though obviously you may have difficulty understanding this.
 
  
Templates can contain pretty much anything a normal Wiki page can contain. The <nowiki>{{Help}}</nowiki> template on the top right of this page will automatically add this article to the [[:Category:Help|Help Category]] for example.
+
Each | followed by a label is a parameter name. The text after the = indicates the value. Line breaks between each parameter are entirely optional and serve mainly to make larger templates more legible when called.
  
You do not need the "Template:" prefix to call a template however - the two sets of curly brackets do that for you.
+
Depending on the template, the parameters may not be given explicit labels, and can be given without names as long as they are in the right order:
 +
<nowiki>{{car-salesman|Fred|46|Lots}}</nowiki>
  
== Creation ==
+
Will work the same, assuming that |name, |age, and |car-count are '''always''' the 1st, 2nd, and 3rd parameters, respectively.  
Template creation is a difficult and potentially dangerous undertaking and can cause gigantic problems across the Wiki if done improperly. First off, learn these:
 
  
* MFGG Wiki does not have #if statements installed.
+
Do not mix names and no-names in the same template include; the server will have trouble figuring out which is which, and so will any human editors.
* <nowiki><noinclude> and </noinclude> are used to put guidance and clarification on a Template page that doesn't show when it's called by another page</nowiki>
 
* <nowiki><includeonly> and </includeonly> are used to make templates categorize pages without going into the category themselves.</nowiki>
 
  
  
  
Secondly, templates are similar to functions in other languages in that they may be passed parameters. For instance, if you call a Template with this:
+
== Creation ==
 +
Template creation is a difficult and potentially dangerous undertaking and can cause gigantic problems across the Wiki if done improperly. First off, the following points:
  
 +
* <nowiki><noinclude> and </noinclude> will prevent the content between them from showing when the page is included. This is useful for adding documentation on the form and use of the template, and for categorizing the template without categorizing pages that include it.</nowiki>
 +
* <nowiki><includeonly> and </includeonly></nowiki> cause the content between them to ''only'' display and parse once included. This is primarily used for categorizing including pages without categorizing the template.
 +
 +
=== Using parameters ===
 +
Recall that when calling a template, you may pass it a parameter:
 
  <nowiki>{{Template
 
  <nowiki>{{Template
|this = hard
+
|param = anything
}}</nowiki>
+
}}</nowiki>
 +
 
 +
In the template page, this parameter is called by its name in triple brackets.
 +
<nowiki><tr>
 +
  <td>{{{param}}}</td>
 +
</tr></nowiki>
  
You will have passed a parameter named '''this'''. In the template, you would call this parameter with triple brackets:
+
However, doing this will cause the text <nowiki>{{{param}}}</nowiki> to display if such a parameter was not given. To rectify this, place a | after its name, and after the | place alternate text to display when no value is given:
  
  <nowiki>{{{this}}}</nowiki>
+
  <nowiki><tr>
 +
  <td>{{{param|N/A}}}</td>
 +
</tr></nowiki>
  
However, if there is no value for the parameter, it will display <nowiki>{{{this}}}</nowiki> instead of a value. To fix this, you use | to add a default:
+
Here, if |param was passed, its value will display. If no |param was given, "N/A" will display.
  
<nowiki>{{{this|}}}</nowiki>
 
  
Whatever is after the | will be shown if <nowiki>{{{this}}}</nowiki> is not passed to the template. If there's nothing, then it will be blank, instead of showing <nowiki>{{{this}}}</nowiki>.
 
  
You can also nest parameters, which will let you put aliases:
+
You can also use other parameters as the alternate text, which lets you create many aliases for the same value:
  
  <nowiki>{{{birthday|{{{dob|Unknown}}}}}}</nowiki>
+
  <nowiki><tr>
 +
  <th>Date of birth</th>
 +
  <td>{{{birthday|{{{dob|}}}}}}</td>
 +
</tr></nowiki>
  
 
In this case, passing either <nowiki>{{{birthday}}}</nowiki> or <nowiki>{{{dob}}}</nowiki> will have the same effect.
 
In this case, passing either <nowiki>{{{birthday}}}</nowiki> or <nowiki>{{{dob}}}</nowiki> will have the same effect.
  
This can get very confusing. Do it sparingly.
+
This can get confusing; try not to add many aliases for one parameter.
 
 
 
 
  
You can also call a template with unnamed fields:
+
=== Unnamed parameters ===
  
 +
If you were, for some reason, to call a template and pass parameters without any names:
 
  <nowiki>{{template|Bob|24|Golf}}</nowiki>
 
  <nowiki>{{template|Bob|24|Golf}}</nowiki>
  
In which case, each parameter is called by number, starting with 1:
+
Then you would have to call them by number in your template:
  
<nowiki>{|
+
  <nowiki><table>
|-
+
  <tr>
! {{{1|No name}}}
+
    <th colspan='2'>{{{1|NO NAME}}}</th>
|-
+
  </tr>
! Age
+
  <tr>
| {{{2|Unknown age}}}
+
    <th>Age</th>
|-
+
    <td>{{{2|Not given}}}</td>
! Interests
+
  </tr>
| {{{3|Unknown interests}}}
+
  <tr>
|}</nowiki>
+
    <th>Hobbies</th>
 +
    <td>{{{3|N/A}}}</td>
 +
  </tr>
 +
</table></nowiki>
  
It is generally a good idea to use numbered parameters for templates that only require one or two. For templates with many fields, especially if some of them are optional, it is better to ask for named parameters.
+
It is generally a good idea to use numbered parameters for templates that only require one or two, so that calling them is faster. For templates with many fields, especially if some of them are optional, it is better to ask for named parameters.
  
 
==Types==
 
==Types==

Latest revision as of 07:23, 8 February 2011

Wiki Basics
Wiki Editing
Wiki Formatting
Wiki Experience

Templates are special pages existing within the "Template" namespace. These pages are designed explicitly for other pages to call them (or rather, "include"), meaning you can use one page as content in many pages.

For instance, this page calls Template:Help, which may be edited to change the displayed help contents on all of the help pages that include it at once.

Templates are particularly useful for quickly embedding otherwise complex table code, while needing to give only the most vital information (see Template:MFGGer.)

Inclusion

Basic templates

Templates are included in a page with their name (WITHOUT THE "TEMPLATE:" PART) between point-curled brackets, as in {{-name-}}:

{{Help}}

This calls Template:Help. Except for the first letter ("H" in this case), all template names are case-sensitive. (e.g. {{Help}} and {{help}} are the same - but {{hElP}} is not)

Parameters

The true power of templates is that they may be passed parameters when included, which they will utilize in some manner depending on the template itself. For instance, calling the (fictional) "Template:Car-salesman" with relevant information may look something like:

{{car-salesman
|name=Fred
|age=46
|car-count=Lots
}}

Each | followed by a label is a parameter name. The text after the = indicates the value. Line breaks between each parameter are entirely optional and serve mainly to make larger templates more legible when called.

Depending on the template, the parameters may not be given explicit labels, and can be given without names as long as they are in the right order:

{{car-salesman|Fred|46|Lots}}

Will work the same, assuming that |name, |age, and |car-count are always the 1st, 2nd, and 3rd parameters, respectively.

Do not mix names and no-names in the same template include; the server will have trouble figuring out which is which, and so will any human editors.


Creation

Template creation is a difficult and potentially dangerous undertaking and can cause gigantic problems across the Wiki if done improperly. First off, the following points:

  • <noinclude> and </noinclude> will prevent the content between them from showing when the page is included. This is useful for adding documentation on the form and use of the template, and for categorizing the template without categorizing pages that include it.
  • <includeonly> and </includeonly> cause the content between them to only display and parse once included. This is primarily used for categorizing including pages without categorizing the template.

Using parameters

Recall that when calling a template, you may pass it a parameter:

{{Template
 |param = anything
 }}

In the template page, this parameter is called by its name in triple brackets.

<tr>
  <td>{{{param}}}</td>
</tr>

However, doing this will cause the text {{{param}}} to display if such a parameter was not given. To rectify this, place a | after its name, and after the | place alternate text to display when no value is given:

<tr>
  <td>{{{param|N/A}}}</td>
</tr>

Here, if |param was passed, its value will display. If no |param was given, "N/A" will display.


You can also use other parameters as the alternate text, which lets you create many aliases for the same value:

<tr>
  <th>Date of birth</th>
  <td>{{{birthday|{{{dob|}}}}}}</td>
</tr>

In this case, passing either {{{birthday}}} or {{{dob}}} will have the same effect.

This can get confusing; try not to add many aliases for one parameter.

Unnamed parameters

If you were, for some reason, to call a template and pass parameters without any names:

{{template|Bob|24|Golf}}

Then you would have to call them by number in your template:

 <table>
  <tr>
    <th colspan='2'>{{{1|NO NAME}}}</th>
  </tr>
  <tr>
    <th>Age</th>
    <td>{{{2|Not given}}}</td>
  </tr>
  <tr>
    <th>Hobbies</th>
    <td>{{{3|N/A}}}</td>
  </tr>
</table>

It is generally a good idea to use numbered parameters for templates that only require one or two, so that calling them is faster. For templates with many fields, especially if some of them are optional, it is better to ask for named parameters.

Types

There are several types of template as you could probably guess. MFGG currently has groups for

Some special ones have been covered in the rest of this guide.