User:Ranga/Guides/Templates

From Snoutslouts

Jump to: navigation, search

Contents

What is a mediawiki template? (last updated Jan 16, 2009)

Source: http://knowledgethoughts.com/wiki/Mediawiki_templates

In Mediawiki language a template is a chunk of automatically generated text.

They're immensely useful when you're building a wiki as they:

  • help enforce a consistent look & feel
  • contribute to a well categorised wiki
  • improve the ease of use


Take for example this article on John o' Groats (picture coming soon). It has a big section down the right hand side of the screen which shows a picture on the ground, its location on a map, the local population and other vital statistics. This style is not used only for the John o' Groats page but for locations across the planet.

To avoid having to create this box using Mediawiki's horrible table format, the page editors have created a template (specifically the Infobox UK place template). It's a parameterised bit of text, using Mediawiki markup that takes small chunks of text and outputs a nicely formatted table.

If you're looking to change the overall look and feel of Mediawiki, then "template" is the wrong term. What you want is a called a skin. I'll say right now that I don't have any good examples of how to do this on this site. As of 16 Jan 2009 I promise to post some content and the links I used to help "skin" knowledgethoughts.

If you're looking for help structuring a wiki for commercial use then I suggest you send me an email as I'm available for hire.

Creating templates in mediawiki

Templates in mediawiki are a way to include the text of one page in another, with the twist that you can pass parameters (text) to the template which it can then display or use.

So, our exploration of templates will start from basic, to a more complex.

  1. A simple template with no parameters
  2. A simple template with one parameter
  3. A template with five parameters
  4. A template which categorises your page for you (not yet started)
  5. Some sample templates (also not started yet)
    1. Basic Infobox
    2. Infobox with categories
    3. Pretty callout

If you have questions on how to create a template to do something else, try sending me an email.

I'm not here to do it for you, but I might have some thoughts to share.

Creating a template

Let's say we want to create a template which will contain the text ”This page is authorised text"".

The fastest way to create a template is by typing

 {{Name of template}} 

in the document where you want the template text to be inserted and pressing save.

You will then have a red link in the page for the name you gave the template.

Click the red link to create the template.

Now, type the text ”This page is authorised text" in the the template document and press save. A template is just like any other wiki page, except it is in the "template namespace". That sounds complicated, but just gives you a way to collect them away from other content. Note that the article name displays as template:Name of template.

So, if you go back to your original page, you will see that the marker in the page with the name of the template in curly braces has been replace with the text in the template. If you edit that page, you will see the marker is still there - so if you change the text in the template, this page will automatically display the changed text from the template.

Wikipedia use a range of templates like this, called stub templates, for a range of various things. SeeStub template messages on WikiPedia.

A simple template with one parameter

Often you may want to create a template for display and layout purposes, and pass it some text which is specific to the calling page.

Type

{{Warning|This page needs your help}}


in your sandbox document and press save. The text "Warning", the name of the template, is red. Click the link, and we are now creating the template. Type

'''{{{1}}}'''
----

And press save. This is saying, bold the text that is passed to the template (ie three curly braces around the 1), and put a line beneath it. The calling page passes parameters by using vertical pipes after the name of the template.

Save the template, and go back to you sandbox page.

You wil see a bolded warning has been inserted in the page by your use of the Warning template you have created.

Edit the sandbox page, and change the text of the warning you are passing to the template and see the result.

For simple templates, passing the variables in the same line separated by pipes is an easy and effective way to instruct templates.

A template with two parameters

For a template with two parameters, the calling page would have

{{Warning|Parameter 1|Parameter 2}}

In the template, the two variables received are referred to as

{{{1}}} And {{{2}}}

A template with five parameters

When you start to get templates with more than one or two variables (like the infoboxes seen on wikipedia or travel wiki), then separating the variables by vertical pipes, and using numbered parameters in the template gets a bit difficult visually to type etc.

In this situation, you use labels for the parameters.

For example, in the calling page

 {{PageWarning
 |Text = Please confirm sources
 |Action = This page will be removed.
 |Contact = Paul Jones}} 


So, this calls the template and passes three names variables. The naming makes it easier to create and maintain the template.

So, now create the template PageWarning as follows

"Content quality is important to us at Acme, and this page required your attention.

 {{{Text}}}

 If you do not address this issue, {{{Action}}}.

 For further information, call [[{{{Contact}}}]].

 ----"

Note that the text of a variable can be included in a paragraph, or in a table cell etc. Also, note that you can make the text passed a link to an article by surrounding it in square braces.

Of course, the variable you pass can itself have the braces, so you could have this call to our template

{{PageWarning
 |Text = See the story on [http://cnn.com CNN]. 
 |Action = This page will be removed.
 |Contact = Paul Jones}}

Essentially any wikitext can be passed to the template.

Adding categories as well

A neat thing about templates is you can use them to display text, and also to put category tags into the pages in which they are included.

If you type

[[Category: Pages with warnings]]

into your warning template, and page into which it is included will now automatically get that category tag. This is a very powerful way to automatically tag content.

Now, just by doing this, the template article itself will now show up in the category - which is probably not desirable.

Fortuneately, there is a way to deal with this rather easily.

In the template, and text that you want included ONLY when the template is used in another page (ie not to be impact the template page itself) you include in includeonly tags.

So,

<Includeonly>

 [[Category: Pages with warnings]]
 
 </includeonly>

Ok, so that now puts the category tags only in the article that calls the template, and the template no longer appears in the category "Pages with warnings”.

But we do want to have all the warning templates collected together. To do that, we want the template to have a particular category, but not to assign that category to every page that uses this template. In other words, we want to be able to indicate some text that is NOT to be included in calling pages.

Fortuneately, there is a way to deal with this rather easily too.

In the template, any text that you DO NOT want included when the template is used in another page (ie to impact the template, but not the pages in which it is included) you include in noinclude tags.

So,

<Noinclude>
 
 [[Category: Warning templates]]

 </noinclude>

A word about naming and categories

The names of the templates should be kept small for templates that use non labelled parameters so that the text in the article does not look odd, and for more complex templates, a naming convention is worth considering.

For example

 {{Warning}}
 
 {{New}}

 {{Infobox/Country}}

 {{Infobox/Topic}}

A word about editing templates

When you edit an article that references a template, you will notice that under the edit box there is a vertical listing of all of the templates used in the current document. This is the easiest way to locate a template document for editing. Edit a document where you know it is included, scroll down and click the name of the template you are in interested. The template will then display in view mode. Click edit, and voila, you are now editing the template!

Further reading

See the description from Mediawiki.org.

Personal tools