User:Soni/Templates adoption
Templates
[edit]Hello, I'm TheOriginalSoni and I'll be your teacher for this lesson on templates.
Let's just start with the basics now - Any page that has a Template:
before it's name is in the Template namespace, and is a template. They function to help make things a lot simpler when you are editing any page. A template is simply a different page whose contents can be seen from another page by calling it.
Lets say you have a page Calling page where you want some text to appear. But you do not want to deal with the huge mess of text in the editing window, and would want it replaced by just a few characters which would do the same work. That is when you use a template.
First, we make a page (Lets say Template:Template page) where you have whatever information you wanted to view on Calling page. Since it is in the Template namespace, we can use it as a template, and call it from Calling page now.
To call the template, you use the following code on Calling page - {{Template name}}
. When you do that, whatever is in Template:Template page gets automatically displayed in place of {{Template name}}
every time you open Calling page. Now if you want to change some text, you have to change Template name, and the change will be automatically shown on Calling page whenever you open it.
Simple enough? Good. Because now we will be learning some more cool stuff with Templates.
The first thing you now need to know that you can use a template even without putting it into the Template namespace. But then, you will have to mention the namespace of whichever template you are using. For example, the table that you see below is a template at User:TheOriginalSoni/Templates adoption 1. Then how did I get it to work? By using the full name of the template- {{User:TheOriginalSoni/Templates adoption 1}}
.
Here is a table of what happens when you call these templates-
What I type | What appears | Comments | ||
---|---|---|---|---|
{{user en}} |
|
This calls Template:User en. All content there (that is marked to be included, see below) appears where I type the template code. | ||
{{Soni DYK}} | {{Soni DYK}} | I get a red link because no page exists at Template:Soni DYK. | ||
{{User:Saoshyant/Userboxes/User oops}} |
|
When I specify the User: namespace, the userbox I have at that location appears. Thus, a template does not have to be in the Template: namespace to work. | ||
{{User DYK}} |
|
I get a {{{1}}} where a number should appear. This is due to the fact that I did not specify a parameter in that template. |
The second thing to know is parameters. For some templates, you might want to change the way it displays for different values you specify. For that, you use parameters while calling the template. To use that, you simply call your template this way - {{Template page|foo}}
. This code will call the template, but with the parameter "1" getting the value of "foo" when called. While making the template, you can give the parameters names or numbers. When you use names, you have to write the parameter name while calling it before specifying its value. {{Template page|name=foo}}
will now give a value of "foo" to the parameter "name". When using numbers, you may choose to skip the parameter's name, but you can't do that for named parameters.
In the template page, there are several codes you can add to do some special things, including adding a parameter. This table shall explain what they do.
Code | What it does |
---|---|
{{{1}}} | Causes a parameter "1" to display at that location. |
{{{name}}} | Causes a parameter "name" to display at that location. |
{{{1|foo}}} | Sets a default value "foo" for parameter "1", which prevents the parameter from displaying as it does in the userbox above. This can even be blank: {{{1|}}} |
<includeonly>foo</includeonly> | Causes the text "foo" to only appear when the template is called. It will not appear on the template page, or in previews when editing the template. |
<noinclude>foo</noinclude> | Removes the text "foo" from the template when it is called. Any text within those tags will appear on the template page, but not on the page calling it. All documentation of the template [Which is simply an explanation of how the template works] is used with these tags. |
{{{1|lorem ipsum}}} <noinclude>dolor sit amet</noinclude> <includeonly>etc...</includeonly> | When this template is called, it will display parameter 1 first, followed by "etc...". If parameter 1 is not defined, the template will display "lorem ipsum etc..." |
Some templates can be used with parser functions such as #if: These functions to apply certain conditions to the code and make the template appear differently based on what the function is. These functions can help you to create some rather advanced templates, but often get exceedingly complicated and should only be edited by those users who fully understand how they work. Since these are rather complex, they will not be covered in your exam, but if you'd like we can cover them after we've completed the other topics.
The last thing you need to know about templates is Substitition. Substitution, or "subst'ing" a template replaces the curly braced call by the actual template text. To do this, add the code "subst:" at the beginning of the call: {{subst:Template name}}
. When this is done, the MediaWiki software will replace that call with the template's code on saving the page. This causes the template to lock in place - however it was when you called it, is how it's going to be from then on. The page at WP:SUBST gives details on what templates should, must, or must not be substituted. When writing templates, it can also be useful to enclose the subst: code within <includeonly> tags. See below.
Code | Displays | Comments |
---|---|---|
{{CURRENTTIME}} | 10:10 | This template updates every time you load the page. |
{{subst:CURRENTTIME}} | 21:51 | This template is substituted, so is stuck on the time I saved this page. |
{{<includeonly>subst:</includeonly>CURRENTTIME}} | 10:10 | Here, the template acts as though it were transcluded on the source page of this lesson. However, it was substituted when I placed this on my page, and so is stuck at the time shown. |