User:Waitingforspring/Templates

This is a short tutorial on template coding. I know there is already an official Help article on it, but I'd like to keep this simple and with many examples, especially to help new users.

What is a Template?
A template is a piece of stored content (some text, a banner, a table, a userbox) that can be included in any article by simply posting its name. For instance, if you want to add a certain pre-formatted element to several articles, create a Template with the code, and post the template in the articles (instead of the code itself).

Creating and Using a Template
Create a blank page with its name beginning with "Template:". For example, "Template:Hello". Write your code in that page, and save it. For instance, paste this code in Template:Hello: Good afternoon!

Now, whenever you write in an article, the following will appear...

Nice, eh? Now, let's see some more advanced usage.

Parameters
A handy feature of templates is that they can accept parameters; values that modify the displaying of the template, without you needing to modify the entire code every time. Let us modify the above template, so it will accept an user input.

Numbered Parameters
You can make the template to accept parameters by adding,  etc to its code, values that will be read as user input. Modify the above code as follows: Good afternoon, !

You can see that I added " " there. The template will read a parameter (separated with a | from its name) and display it at that location.

The style problem you might observe: if you did not provide a parameter, the template displays. You should add a default value that is displayed when no parameter is entered as  : Good afternoon, !

Now if no text is entered, "User" will be displayed (that is a default value I defined next to the "1").

You can add multiple parameters; for instance, let's add a second one for the time of day: Good, !

The template will read two parameters, separated with |, and display it at the indicated location. If no text is entered, the default values ("User" and "afternoon") will be displayed. {| cellpadding="3"
 * style="width:220px"|You write this...
 * style="width:220px"|This appears...


 * }

Named Parameters
Instead of numbers (1, 2 etc), you can use strings to refer to parameters. For instance, change the above code to: Good, !

Now, when calling the template, it is not enough to input parameters with pipes between them; we need to specify their names:

The advantage of this method is when you have lots of parameters to input in a template (such as Template:Charcat); with named parameters, you can easily overview which data did you pass to the template.

Magic Parameters
These are parameters where the result is pre-defined, instead of given by the user - for example, the current time. A list of these is here. These are always written in uppercase. Now write this in the template: Hello! Current time is. You are reading on.

And the result: