Community Central

User blog:Gamedezyner/Templates - A Brief Introduction


Templates - A Brief Introduction

So lately I've been seeing a lot of talk about templates. I thought I'd write up a little bit of info to help people understand what they are and how they work!

What is a Template?

You might ask, "What is a template?"

The definition of template (in computing) is
a preset format for a document or file, used so that the format does not have to be recreated each time it is used.

In Wikia terms, it's a page that contains content or formatting that will be used a lot, so we include that page on other pages instead of re-writing the content every time we need it.

Using Templates on Wikia

To use templates on Wikia, simply put the name of the template inside double curly brackets ({{}}). If the template uses parameters, seperate them with a vertical pipe character (|). The vertical pipe character is usually typed by holding shift and pressing the backslash key (\). The result would look something like this: {{TemplateName|Parameter1|Parameter2}}. Including this code on a page "calls" the template, causing Wikia to place the template on the page.

Types of Templates

There are two types of templates you can use:

Static Templates

These are templates whose content is the same on every page it's used. These are great for special messages, banners, and even signatures!

Take a look at this Static Example. The code on that page is: '''I like desserts!'''.

I like desserts!

This template is used by placing {{Static Example}} on any other page.

Dynamic Templates

These are templates whose content changes depending on the parameters set when it's used. These are great for infoboxes, and other content that uses odd or complex formatting.

Take a look at this Dynamic Example. The code on the template page is: '''I like {{{1|nothing}}}!'''. The part inside curly brackets is a parameter, which defaults to say "nothing" if you don't give it a value.

I like nothing!

This calls the template without any parameters like so: {{Dynamic Example}}

This template accepts a parameter, so we can change its content on this page by adding a parameter to the call:

I like chocolate cake!

This example calls the template using a parameter like so: {{Dynamic Example|chocolate cake}}

Creating Templates

A static template is really just a page in the Template namespace. If you just need to repeat some content on a few pages, then you can simply create a page and add the content you wish to use. If you want to create a dynamic template, you will need to use parameters. A parameter is a variable that is set when you call the template. When writing the template page, you can include parameters using triple curly brackets ({{{}}}). When the template is called, this is replaced by the value in the template call.

Using Numbered Parameters
If you only plan to use a few parameters, you can simply number them. This will use the order that they are entered in the template call. Say I like to discuss my likes and dislikes a lot. I would create a page at Template:LikeDislike, and make its content I like {{{1}}}, but I don't like {{{2}}}. Then on a page about desserts I would put {{LikeDislike|chocolate cake|vanilla pudding}}. When that page is saved, I would get I like chocolate cake, but I don't like vanilla pudding.
Using Named Parameters
If you want to use a lot of parameters, you can name them. This will require you to call them by name, but won't pay attention to the order. Using the same idea of likes and dislikes, I could create the same template page, and make its content I like {{{likes}}}. I don't like {{{dislikes}}}. My favorite is {{{favorite}}}.. Then I could call it by putting {{LikeDislike|likes=chocolate and strawberry|dislikes=vanilla and butterscotch|favorite=cherry}}. In the end I would get I like chocolate and strawberry. I don't like vanilla and butterscotch. My favorite is cherry.
Default Values for Parameters
If you don't specify a parameter, it will be empty. This can be avoided by adding a vertical pipe (|) after the parameter number/name on the template page, then specifying a default value. In the dynamic example above, I added "|nothing" after the 1 to make it default to say "nothing" if the parameter isn't specified.

Advanced Stuff

You can do a lot of amazing things with templates. So much that I couldn't dream to go over all of it. I will, however, give a quick explanation of some of the other things that work in templates.

Special Tags

There are some special tags for use on template pages which can help you control what is shown when it is called:

The <noinclude></noinclude> tag allows you to prevent a segment of the template page from being shown on another page. This is useful for placing instructions about how to use the template on the template page itself. If you surround the instructions with these tags, they will not show on other pages when the template is used.
The <includeonly></includeonly> tag allows you to make a segment of the template page only show when the template is used on another page. This is useful for automatically placing categories on pages when the template is useed. If you surround the category link with these tags, it will only categorize other pages when the template is used.
The <onlyinclude></onlyinclude> tag allows you to make only a certain segment of the template page show when the template is used.

Magic Words

Magic Words are special templates that automatically get replaced with certain values. An example would be {{NUMBEROFPAGES}}. This Magic Word will always give the number of pages on the wiki it is used on. It automatically gets this number every time the page is visited.

Code: There are {{NUMBEROFPAGES}} pages on this wiki!
Result: There are 918,450 pages on this wiki!

Magic Words can be very useful in some types of templates because they allow you to reference things like the title or the last editor of the page that the template is used on. You can read more about Magic Words, on this page.

Parser Functions

Some of the most complex templates make use of parser functions. These special functions allow you to use logic to help determine what content is shown when the template is called. A great example would be the {{#if:}} function. This function checks to see if the first value given is true or false, then outputs the correct result. A zero is treated as false, while any other number is treated as true.

Example 1
Code: {{#if: 1|I like chocolate!|I do not like chocolate.}}
Result: I like chocolate!
Example 2
Code: {{#if: 0|I like chocolate!|I do not like chocolate.}}
Result: I do not like chocolate.

There are many other parser functions that do some pretty cool things. You can read more about parser functions on this page.