Help:Templates: Difference between revisions

Jump to navigation Jump to search
Sectioning, clarification etc
(Unnamed parameters have to be used in specified order + Category suppression)
(Sectioning, clarification etc)
Line 1: Line 1:
'''Templates''' are pages that can contain complex pieces of wiki markup that can be reused on other pages with relative ease.  On this help page we will go through what a template is, how to find and use them and editing and creating templates.  Finally we will have a quick look on some of the things that can be used in more advanced templates.
'''Templates''' are pages that can contain complex pieces of wiki markup that can be reused on other pages with relative ease.  On this help page we will go through what a template is, how to find them and use them as well as editing existing and creating new templates.  Finally we will have a quick look on some of the things that can be used in more advanced templates.


== Basics ==
== Basics ==
Line 28: Line 28:
== Using templates ==
== Using templates ==
=== Finding templates ===
=== Finding templates ===
Most templates can be found in [[:Category:Templates]] and its subcategories.  There is also lists of some of the templates, see [[Help:List of messagebox templates]] and [[Help:List of infobox templates]].
There are several ways to find a template to use:


Another way to find templates is when you find a page using templates you are interested in using.  If you open that page for editing you will on the very bottom of the page find a list of the templates used by that page and the templates those templates are using.
;Using categories:  Most templates can be found in [[:Category:Templates]] and its subcategories.
 
;Using lists of templates:  There is also lists of some of the templates, see [[Help:List of messagebox templates]] and [[Help:List of infobox templates]].
 
;Finding templates through a page they are used within:  Another way to find templates is when you find a page using templates you are interested in using.  If you open that page for editing you will on the very bottom of the page find a list of the templates used by that page and the templates those templates are using.
 
;Finding templates similar to another one:  Links to similar templates can often be found at the bottom of a templates documentation.
 
;Searching for contents of a template:  As a last resort you can search for using possible contents of a template, typing "Template:<whatever>" in the search box.  Instead of going straight to the page as you would do if you would knew its name, you will get to a page of search results with pages that might contain the template you are looking for.


=== Template parameters ===
=== Template parameters ===
Most templates take arguments, called parameters.  Some templates do not.  Some parameters may be optional, some mandatory.  Mandatory parameters must always be used, while optional parameters doesn't have to.  There are also unnamed and named parameters.  Named parameters are always used in key=value pairs, like for example <tt>month={{CURRENTMONTHNAME}} {{CURRENTYEAR}}</tt>.
Most templates take arguments, called parameters.  Some templates do not.  Some parameters may be optional, some mandatory.  Mandatory parameters must always be used, while optional parameters doesn't have to.  There are also unnamed and named parameters.  Unnamed parameters are sometimes called numbered parameters as they have to be used in a certain order and ar numbered within the template source.  Named parameters are always used in key-value pairs, like for example <tt>month={{CURRENTMONTHNAME}} {{CURRENTYEAR}}</tt>.


In some of the template documentation the following convention is used to illustrate how parameters can be used:
In some of the template documentation the following convention is used to illustrate how parameters can be used:
Line 41: Line 49:
In most cases it is possible to use any kind of wiki markup in the parameters, for example wiki links.
In most cases it is possible to use any kind of wiki markup in the parameters, for example wiki links.


One thing to mention here is that unnamed parameters must be used in a certain order, though they can if not mandatory be left empty.  Take for example the following template:
As said above the unnamed parameters must be used in a certain order, though they can if not mandatory be left empty.  Take for example the following template:
  <nowiki>{{</nowiki>'''template'''<nowiki>|</nowiki>''parameter 1''<nowiki>|</nowiki>''parameter 2''<nowiki>|</nowiki>''parameter 3''<nowiki>|</nowiki>''parameter 4''<nowiki>|</nowiki><nowiki>}}</nowiki>
  <nowiki>{{</nowiki>'''template'''<nowiki>|</nowiki>''parameter 1''<nowiki>|</nowiki>''parameter 2''<nowiki>|</nowiki>''parameter 3''<nowiki>|</nowiki>''parameter 4''<nowiki>|</nowiki><nowiki>}}</nowiki>


If we would be to use only the last two parameters we would have to to like this below:
Usually the most commonly used and/or mandatory parameters are the first one or ones, but if we just as an example would be to use only the last two parameters we would have to do like below:
  <nowiki>{{template|||parameter 3|parameter 4}}</nowiki>
  <nowiki>{{template|||parameter 3|parameter 4}}</nowiki>


=== Template documentation ===
=== Template documentation ===
When you know what template you want to use, you might want to take a look at its documentation.  You will find the documentation on the template's page.  The documentation describes the purpose of the template and how to use it, as well as link to related templates.  Unfortunately not all templates are documented.
When you know what template you want to use, you are recommended take a look at its documentation, unless you have already done that earlier.  You will find the documentation on the template's page.  The documentation describes the purpose of the template and how to use it, as well as link to related templates.  Unfortunately not all templates are documented.


Many templates have a ''Known issues'' section in the template documentation.  Some templates have innate peculiarities that can not be fixed, but can be worked around.  One of the more common issues is that a few characters that are used to either build tables or are used within templates will break a template, those characters are a vertical stroke and some combinations of it, <tt>|</tt>, <tt>||</tt> and  <tt>|-</tt> and the equal sign <tt>=</tt>.  The usual workaround are to use the templates {{tl|!}}, {{tl|!!}}, {{tl|!-}} and {{tl|{{=}}}}.
Many templates have a ''Known issues'' section in the template documentation.  Some templates have innate peculiarities that can not be fixed, but can be worked around.  One of the more common issues is that a few characters that are used to either build tables or are used within templates will break a template, those characters are the equal sign <tt>=</tt> and a vertical stroke and some combinations of it, <tt>|</tt>, <tt>||</tt> and  <tt>|-</tt>.  The usual workaround are to use the templates {{tl|{{=}}}}, {{tl|!}}, {{tl|!!}} and {{tl|!-}}.


The template documentation contains a list of the parameters the template uses that can be copied and pasted to the page you are editing, sometimes even with some of the parameters filled in, like for example this template:
The template documentation contains a list of the parameters the template uses that can be copied and pasted to the page you are editing, sometimes even with some of the parameters filled in, like for example this template:
Line 58: Line 66:
In the template documentation there is also a list of the parameters and how they are supposed to be used and there are often examples to illustrate the different ways the parameters can be used.
In the template documentation there is also a list of the parameters and how they are supposed to be used and there are often examples to illustrate the different ways the parameters can be used.


== Editing templates ==
== Creating new templates ==
=== Creating new templates ===
=== Choosing a good name ===
Consider giving the template a meaningful name.  If you are mimicking a template on another wiki, using its name might be a good idea.  If the template will be used very often it might actually be a good idea to use a very short name, but proper documentation will then be even more important.  Do note that parameter names are case sensitive except for the first character.
Consider giving the template a meaningful name.  Usually one would try to only have the initial character upper case.  If you are mimicking a template on another wiki, using its name might be a good idea.  If the template will be used very often it might actually be a good idea to use a very short name or an abbreviation, but proper categorisation and documentation will then be even more important.  Do note that parameter names are case sensitive except for the first character.


=== Search first ===
To create a new template, please start by using the search function.  This way you will find if there already is a template with that name or if there is one with a very similar name.  If there is no template with that name you will be presented with the option to create that page.  Click on the red link to open up a edit window.
To create a new template, please start by using the search function.  This way you will find if there already is a template with that name or if there is one with a very similar name.  If there is no template with that name you will be presented with the option to create that page.  Click on the red link to open up a edit window.


=== Things to include ===
Two important things to add to your template is template documentation, preferably using {{tl|informative template}} and if possible to put the template in a more fitting category than [[:Category:Templates]].
Two important things to add to your template is template documentation, preferably using {{tl|informative template}} and if possible to put the template in a more fitting category than [[:Category:Templates]].


Unless it is a very simple template it can be a good thing to start writing the documentation as this might help you figuring out the last few things in what you want the template to do and how you want it to do that, and then more or less use the documentation as reference when typing the template itself.
Unless it is a very simple template it can be a good thing to start by writing the documentation as this might help you figuring out the last few things in what you want the template to do and how you want it to do that, and then more or less use the documentation as reference when typing the template itself.


Do remember that you can, and should use the preview button now and then, or at least one before saving, in order to catch any errors before saving.  While the template always can be fixed if there are errors it is better to have an extra preview than an extra editDo note that the template will be lost if you do not save it.
=== Preview often ===
Do remember that you can, and should use the preview button now and then, or at least one before saving, in order to catch any errors before saving.  One way of debugging a template is to add obvious clues as to where in a template something got wrong, for example 1?, 2?...  This can be really helpful in some cases,  though in other cases it will be obviousSome common mistakes are the same ones as on article pages:  Missing end markers in tables and missing end tags or end tags with missing slashes.


=== Editing templates ===
While the template always can be fixed if there are errors it is better to have an extra preview than an extra edit.  Do note that the template will be lost if you do not save it.
 
== Editing templates ==
Editing a template is often as easy as editing a normal wiki page.  Lets say you want to edit a template with the name ''what you always wanted to know''. Either search for the template using the template namespace like ''Template:What you always wanted to know'' or click on a link to get you there.
Editing a template is often as easy as editing a normal wiki page.  Lets say you want to edit a template with the name ''what you always wanted to know''. Either search for the template using the template namespace like ''Template:What you always wanted to know'' or click on a link to get you there.


Line 88: Line 101:
Hello, Dr. Falken!
Hello, Dr. Falken!


You might wonder what that vertical stroke and the text following, <tt><nowiki>{{{</nowiki>1'''|'''<nowiki>}}}{{{</nowiki>2'''|stranger'''<nowiki>}}}</nowiki></tt>, in ''Template:Hello'' does.  Some of you might already have guessed that they are default values.  Having at least an ''empty'' default value is a good practice, as templates otherwise will break when passed an empty parameter.  Here is what happens when the above template is used without parameters:
=== Default values ===
You might wonder what that vertical stroke and the text following, <tt><nowiki>{{{</nowiki>1'''|'''<nowiki>}}}{{{</nowiki>2'''|stranger'''<nowiki>}}}</nowiki></tt>, in ''Template:Hello'' does.  Most of you will already have guessed that they are default values.  Having at least an ''empty'' default value is a good practice, as templates otherwise will break when passed an empty parameter.  Here is what happens when the above template is used without parameters:
  <pre>{{hello}}</pre>
  <pre>{{hello}}</pre>
Hello, stranger!
Hello, stranger!
Line 159: Line 173:
{{main other|[[Category:Hello templates]]}}
{{main other|[[Category:Hello templates]]}}
</noinclude></nowiki>
</noinclude></nowiki>
Apart from <tt>demospace=</tt> some templates will have a <tt>nocat=</tt> parameter that when not empty or with the value <tt>yes</tt> will suppress categorisation altogether.


== Translating templates ==
== Translating templates ==
Line 173: Line 189:
;Tables:  Wiki tables are tricky to understand, but can do a lot for any kind of box template and small layouts.
;Tables:  Wiki tables are tricky to understand, but can do a lot for any kind of box template and small layouts.


;Magic words:  Magic words at first look like templates except for that they mostly are all uppercase.  Some of them are for example <tt><nowiki>{{CURRENTMONTHNAME}}</nowiki></tt> and <tt><nowiki>__NOTOC__</nowiki></tt>.
;Magic words:  Magic words are mostly all uppercase and many of them at first look like templates.  Some of them are for example <tt><nowiki>{{CURRENTMONTHNAME}}</nowiki></tt> and <tt><nowiki>__NOTOC__</nowiki></tt>.


;Parser functions:  Parser functions allow more or less regular programming with conditionals, switches, string formatting etc.  A good hint is that well placed empty html comments can make the code way more readable.  One of them is <tt>#if</tt>, <tt><nowiki>{{</nowiki>'''#if:''' ''string'' | ''passed if non-empty'' | ''passed if empty'' <nowiki>}}</nowiki></tt>.
;Parser functions:  Parser functions allow more or less regular programming with conditionals, switches, string formatting etc.  One of them is <tt>#if</tt>, <tt><nowiki>{{</nowiki>'''#if:''' ''string'' | ''passed if non-empty'' | ''passed if empty'' <nowiki>}}</nowiki></tt>.


;HTML:  It might seem a bit funny to mention it, but sometimes you forget that you can use (some) html tags in a MediaWiki wiki.
;HTML:  It might seem a bit funny to mention it, but sometimes you forget that you can use (some) html tags in a MediaWiki wiki.


;Cascading style sheets:  Like magic words and parser functions can do magic for the functionality of a template CSS can do magic for its appearance.
;Cascading style sheets:  Like magic words and parser functions can do magic for the functionality of a template CSS can do magic for its appearance.
A good hint on getting more readable template source is that well placed HTML comments (<tt>&lt;!-- Comment --&gt;</tt>) will help a lot. 


== External links ==
== External links ==

Navigation menu