User:Leujohn/Adoption/Templates 1
Templates
[edit]Templates allow you to post large sections of text or complicated sections of code while only typing a few characters. Templates work similar to regular links, but instead of [[double square brackets]], you use {{curly braces}}. To call a template, just type the title of the template between the double braces. You don't need to include the "Template:" prefix; the MediaWiki software automatically searches within the Template namespace for what you're looking for. Only if the page you're looking for is in a different namespace do you need to specify it. See below:
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. | ||
{{Hersfold/Userboxes/USTravel}} | Template:Hersfold/Userboxes/USTravel | I get a red link because no page exists at Template:Hersfold/Userboxes/USTravel. | ||
{{User:Hersfold/Userboxes/USTravel}} |
|
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. |
In that last example, 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. One template you can use to welcome new users, Template:W-basic, has several parameters which can customize its appearance. Most of those parameters are named, in that you have to specify to the template what the name of the parameter is when you use it. {{w-basic|anon=true}} sets the parameter "anon" to "true", which generates a message directed towards anonymous users. The advantage to named parameters is that they can be placed in any order, but they must be spelled exactly right or they will not work. The template also uses an unnamed parameter, one which does not have to be specified when it is put into use. Templates automatically assign a numerical name to unnamed parameters when they are used, starting with "1". {{w-basic|message}} sets the unnamed parameter "1" to "message", which is what that parameter is used for in that template. The userbox above can specify the number of states visited with that same unnamed parameter. Unnamed parameters must be in sequential order to work properly, unless you force them to be out of order by using syntax such as {{template|2=foo|1=bar}}. Using "1=, 2=" is also required if the parameter has a = anywhere within (occasionally the case with some external links).
When writing templates, there are some extra tags and codes that have special effects when a template is called.
Code | What it does |
---|---|
{{{1}}} | Causes a parameter "1" 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 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. As a result, any code included in these tags will not be executed until the template is called. |
<noinclude>foo</noinclude> | Removes the text "foo" from the template. Documentation (notes on how to use a template) is always included with these tags so that it is not called along with the template. |
{{{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..." |
Conditional templates allow for use of more intricate templates, with optional parameters or different effects depending on what a certain parameter is set to. They use parser functions such as #if: to apply certain conditions to the code. Use of these functions can allow 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.
I forgot to mention - there are two ways to call a template. Transclusion is simply calling the template as I showed you above: {{template}}. This displays the template where you put the call for it, but leaves the curly braced call in place so that it's easy to remove. This also causes the template to update every time the page is loaded, if it has been edited or has a time-sensitive variable. Substitution, or "subst'ing" a template, causes the opposite effect. To substitute a template, add the code "subst:" at the beginning of the call: {{subst:template}}. When this is done, you are seeing the curly-braced call for the last time, because when you save the page, the MediaWiki software will replace that call with the template's code. 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. This makes things a little difficult to remove, though, as instead of the simple template call, you've probably got lines of code that are now clogging up your article. Depending on how the template it written, it may require subst'ing to work properly, or it may require that it is not subst'ed. 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}} | 14:39 | Template is transcluded, so updates every time you load the page. |
{{subst:CURRENTTIME}} | 21:20 | Template is substituted, so is stuck on the time I saved this page. |
{{<includeonly>subst:</includeonly>CURRENTTIME}} | 11:22 | Here, the template acts as though it were transcluded on the source page of this lesson, User:Hersfold/Adopt/Templates. However, it was substituted when I placed this lesson on the main adoption page, and so is stuck at the time shown. |
This lesson should show you how templates can be really useful for a lot of things. However, we can make templates even more functional and more powerful by having them do different things depending on what the parameters we set are. For more information on that, see the optional lesson on Advanced Templates.
Assignment
[edit]Answer these questions on your adoption subpage:
- How do you call a template?
- How do you place the parameter "1" in a template?