Help:Templates

From YPPedia

For information on the templates standardly used on the YPPedia, take a look at the style guide. Most of the more complex templates should have directions for use on their talk pages.

What are templates, and why are they used?

On a basic level, templates are a way to arrange things. On a more complex level, they can be used to program things. Both sorts of templates will be looked at later on, but it's useful to remember that the main aim of them is to save time and effort.

A basic template will take the following form:

{{Name|1=parameter|2=parameter2}}

At first, it looks quite scary, but if it's broken into bits, it all seems to be quite logical:

  • The {{ and }} parts, often called curly brackets, just indicate where the template starts and ends.
  • The Name part is the name of the template. By typing that part in, it will call the template into use. Each template has a name, and of course, all templates do very different things from each other.
  • The vertical bar, or pipe, | separates the templates into the name and the parameters.
  • The 1=parameter bits are indeed parameters. These simply tell the template what to fill in and where. Often, the 1= bits are omitted, as they're not always needed. However, it's worthwhile knowing that they exist, especially when making templates.

Now let us look at a basic form of template.

Basic template examples

No parameters

Many pages are incomplete. As such, they are marked as stubs, or short articles. They can easily be designated with the stub template. The stub template can be found at the page Template:Stub. The template will always be found at Template:Name where Name is the name of a template.

The template can be called by typing {{stub}} as such:

Stub.pngArr! This article be a stub. Ye can help YPPedia by expanding it.

(It is possible to type {{stub}} without it being substituted, by use of things called nowiki tags. Normally, anything inside curly brackets get converted into a template, but by typing <nowiki>{{stub}}</nowiki>, it won't substitute it.)

On the Template:Stub page, it contains the following code:

<p>[[Image:Stub.png|25px]]''Arr! This article be a stub. Ye can help YPPedia by [{{SERVER}}{{localurl:{{NAMESPACE}}:{{PAGENAME}}|action=edit}} expanding it].''</p><includeonly>[[Category:Stub]]</includeonly><noinclude>{{usage|This template is designed to denote an article is incomplete, and can use more work. If you want to add a stub notice to a [[template:crewstub|crew article]], [[template:piratestub|pirate article]], [[template:buildingstub|building article]] or [[template:flagstub|flag article]], there are other templates for that.}}</noinclude>

It looks pretty ugly, especially if you don't understand HTML. It's much easier to type {{stub}} than all that, especially for a new person. What's more,it's simple to remember.

There's also a third benefit - if the template was changed, it would automatically update on all pages, rather than having to be manually retyped on all pages. The stub article is used many hundreds of times - imagine how much of a Herculean task that would be to update!

Now, an example of a template that uses a parameter:

One parameter

Templates with more parameters are more interesting.

Take, for example, the template Template:Test font. Its contents are the following:

<font color=green>{{{1}}}</font><noinclude> [[Category:Help templates]]</noinclude>

The noinclude and category bits are of no interest here.

What this template does is to take a parameter and turn it green.

Using the template is simple: typing {{Test font|1=Hello}} or {{Test font|Hello}} gives Hello.

Instead of having to write verbose HTML code for green font to emphasise, say, a correct answer, that template can be used. Again, if needed subsequently to change the color of everything to a different shade of green, it could be done with just one edit rather than one for each instance.

Two parameters

Templates with multiple parameters allow for greater variety, but also a greater scope for confusion.

Quite often, templates of the sort like Template:Test writing come about:

<font color={{{color}}}>{{{text}}}</font> <noinclude>[[Category:Help templates]]</noinclude>

(The functional part of this template is in bold)

This template allows the editor to write a block of text in a certain color.

To call this template, the following syntax would be used:

{{Test writing|color=cyan|text=hello}}

This would tell the browser to:

  • Use the Test writing template,
  • Use cyan colored text
  • Use the cyan colored text on the word hello.

This would result in the following:

hello

Equals signs, words and numbers - oh my!

In the template with just one parameter, the template contained the text {{{1}}}. This told the browser to replace {{{1}}} with whatever was assigned as the first parameter.

In the two parameter example, numbers won't work:

{{Test writing|cyan|hello}} gives the funny looking:

{{{text}}}

More detail on this can be found at the Wikipedia help page, at the bottom.

In short:

It is important when calling templates to use the correct words, otherwise things may not always work out.

In fact, there is a way to allow multiple inputs for a certain template. This requires the use of parameter defaults that make things look much more complex. For the interested reader, the following code would allow both a text= color= and a 1= 2= approach to work:

<font color={{{color|{{{1}}}}}}>{{{text|{{{2}}}}}}</font>

Repeated uses of values

Say, for example, that a certain word had to be printed out 10 times in a row. A template such as Template:X10 can handle this.

To use, simply write the following:

{{x10|text}} where text is the text needed to be repeated 10 times.

For example:

{{x10|hello}} yields:

hellohellohellohellohellohellohellohellohellohello

Note that the template cannot add spaces in. A separate template with the {{{1}}}'s with spaces in between would have to be constructed. Alternatively:

{{x10|hello }} yields hello hello hello hello hello hello hello hello hello hello

although this results in a space at the end of the final hello.

Further examples:

{{x10|[[Image:Stub.png]]}} yields Stub.pngStub.pngStub.pngStub.pngStub.pngStub.pngStub.pngStub.pngStub.pngStub.png

The parameter input can even take other templates:

{{x10|{{x10|hello }}}} yields

hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello

This is an easy way to repeat something 100 times without ever having to make a x100 template.

Using clever tricks to save effort and time are the keys to being an excellent template user.

Aside: It would theoretically be possible to create a way to construct a template Xn for any n (where n is a natural number), using just the templates for the prime numbers. Exactly how to do that is again left as an exercise for the interested reader (and the mathematicians!). However, there is limited use for such a thing.


Substitution

Sometimes, there is the need to substitute some text into an article, for example with the RfA page. Doing this is quite simple; all that is needed to be done is to add subst: to the beginning of the template, as such:

{{subst:Test writing|color=green|text=Hello}} gives: Hello, but will actually show:

<font color={{{color}}}>{{{text}}}</font> <noinclude>[[Category:Help templates]]</noinclude>

instead of {{subst:Test writing|color=green|text=Hello}} in the edit page section.

It is worth noting that substitution is irreversible (except for reverting a page); that is, the substitution occurs when a page is submitted, and once submitted, it leaves no characteristic trace of which template it actually came from. Substitution is rare, since it can no longer be controlled by an edit in the template page. Nevertheless, it is useful to know.

See Also

External Links