Help:Templates: Difference between revisions

Jump to navigation Jump to search
Where one can find template documentation, what happens without default values, other small fixes
mNo edit summary
(Where one can find template documentation, what happens without default values, other small fixes)
Line 2: Line 2:


== Basics ==
== Basics ==
=== What is a template ===
=== What is a template? ===
In simple terms you are adding another page, or rather contents from it, into the page you are editing when you use a template.  This is called transclusion.  While the wiki has a special template namespace, indicated by a preceding ''Template:'' in the page title, any page can be used as a template.
In simple terms you are adding another page, or rather contents from it, into the page you are editing when you use a template.  This is called transclusion.  While the wiki has a special template namespace, indicated by a preceding ''Template:'' in the page title, any page can be used as a template.


If the template for example is named Template:Helloworld and contains:
If a template for example is named ''Template:Helloworld'' and contains:
  Hello World!
  Hello World!


Line 42: Line 42:


=== Template documentation ===
=== Template documentation ===
When you know what template you want to use, you might want to take a look at its documentation.  The documentation describes the purpose of the template and how to use it, as well as link to related templates.
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.


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 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|{{=}}}}.
Line 50: Line 50:
  <nowiki>{{</nowiki>'''splitapart'''<nowiki>|</nowiki>''Discussion page section'' <nowiki>|</nowiki>''date={{CURRENTMONTHNAME}} {{CURRENTYEAR}}''<nowiki>}}</nowiki>
  <nowiki>{{</nowiki>'''splitapart'''<nowiki>|</nowiki>''Discussion page section'' <nowiki>|</nowiki>''date={{CURRENTMONTHNAME}} {{CURRENTYEAR}}''<nowiki>}}</nowiki>


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 how it they be used in different ways.
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 ==
== Editing templates ==
Line 72: Line 72:


=== Passing unnamed parameters ===
=== Passing unnamed parameters ===
Passing parameters to templates make them a lot more useful.  Unnamed parameters can be used in the order they are passed, in essence <tt><nowiki>{{{1}}}</nowiki></tt>, <tt><nowiki>{{{2}}}</nowiki></tt> etc.  Consider the following template ''Template:Hello'':
Passing parameters to templates make them a lot more useful.  Unnamed parameters can be used in the order they are passed, in essence <tt><nowiki>{{{1}}}</nowiki></tt>, <tt><nowiki>{{{2}}}</nowiki></tt> etc.  Consider the following template, ''Template:Hello'':
<pre>Hello, {{{1|}}}{{{2|stranger}}}!</pre>
<pre>Hello, {{{1|}}}{{{2|stranger}}}!</pre>


Its usage
Its usage:
  <nowiki>{{</nowiki>'''hello'''<nowiki>|</nowiki>''title''<nowiki>|</nowiki>''name''<nowiki>}}</nowiki>
  <nowiki>{{</nowiki>'''hello'''<nowiki>|</nowiki>''title''<nowiki>|</nowiki>''name''<nowiki>}}</nowiki>


And an example
And an example:
  <pre>{{hello|Dr.|Falken}}</pre>
  <pre>{{hello|Dr.|Falken}}</pre>
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.  At least having an ''empty'' default value is a good practice, as a template otherwise will break when passed an empty parameter.  This happens if the above template is used without parameters
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:
  <pre>{{hello}}</pre>
  <pre>{{hello}}</pre>
Hello, stranger!
Hello, stranger!
And here is what would happen if it would be used without parameters and did not have default values:
<nowiki>Hello, {{{1}}}{{{2}}}!</nowiki>


=== Passing named parameters ===
=== Passing named parameters ===
While using a template with named parameters can be a bit more of typing, they are not that much extra typing when editing a template.  They are however practical some other ways, they can be passed to a template in any order and, more important, they are named (as in not numbered).  That last thing makes them immensely more useful in complex templates.
While using a template with named parameters can be a bit more of typing, they are not that much extra typing when editing a template.  They are however practical some other ways, they can be passed to a template in any order and, more important, they are named (as in not numbered).  That last thing makes them immensely more useful in complex templates.


Using them in a template are not that different than unnamed parameters.  Using the above template, but with named parameters, we could instead type
Using them in a template are not that different than unnamed parameters.  Using the above template, but with named parameters, we could instead type:
<pre>Hello, {{{title|}}}{{{name|stranger}}}!</pre>
<pre>Hello, {{{title|}}}{{{name|stranger}}}!</pre>


Its usage
Its usage:
  <nowiki>{{</nowiki>'''hello''' <nowiki>|</nowiki>''title='' <nowiki>|</nowiki>''name=''<nowiki>}}</nowiki>
  <nowiki>{{</nowiki>'''hello''' <nowiki>|</nowiki>''title='' <nowiki>|</nowiki>''name=''<nowiki>}}</nowiki>


And an example
And an example:
  <pre>{{hello |title=Dr. |name=Falken}}</pre>
  <pre>{{hello |title=Dr. |name=Falken}}</pre>
Hello, Dr. Falken!
Hello, Dr. Falken!
Line 112: Line 116:
;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.  One of them for example is <tt><nowiki>{{PAGENAME}}</nowiki></tt>, another one is <tt><nowiki>__NOTOC__</nowiki></tt>.
;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>.


;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.  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>.
Line 122: Line 126:
== External links ==
== External links ==
* [http://www.mediawiki.org/wiki/Help:Templates MediaWiki template help]
* [http://www.mediawiki.org/wiki/Help:Templates MediaWiki template help]
* Advanced editing help at [http://www.mediawiki.org/wiki/Project:About MediaWiki]
* Advanced template editing help at [http://www.mediawiki.org/wiki/Project:About MediaWiki]
** [http://www.mediawiki.org/wiki/Help:Tables Help:Tables]
** [http://www.mediawiki.org/wiki/Help:Tables Help:Tables]
** [http://www.mediawiki.org/wiki/Help:Magic_words Help:Magic words]
** [http://www.mediawiki.org/wiki/Help:Magic_words Help:Magic words]

Navigation menu