User:Adam mugliston/Adopt/Template beginner

From Wikipedia, the free encyclopedia

Templates for Dummies (and you're not a dummy, so it should help)[edit]

Templates are scary but they're also extremely powerful, and so they're worth having as a module. They do involve a little bit of coding, but I'm sure you can manage a little bit of coding... just a little little tiny bit?

Right, well, now you're thinking about doing some coding, let's look at where they're used on wikipedia. Chances are, you've already used them. Anything you put in curly braces {{ }} is a template. You may have only used them through copying and pasting, but there's a lot that you might have used. {{Reflist}}, {{Infobox}}, {{Category}} and {{Userbox}} are very common ones, along with templated warnings.

What is a template?[edit]

So what is a template? Well, it's bit of "wikimarkup" (wikipedia code) which can be used on other pages. You have the option of "transcluding" it (putting the template in curly braces, {{TEMPLATE_NAME}} ) or "substituting" it (putting it in curly braces, with the key word subst {{subst:TEMPLATE_NAME}}). If you transclude it to a page, any updates to the template will show on the page - and if you look at the wikimarkup (ie press edit), you will only ever see the curly braces and template name. If you substitute it, you will effectively be copying the template output to the page at the point you press save. Further updates to the template will be ignored, and you will be able to edit the markup on the page.

Where do I find templates?[edit]

Wikipedia has a specific namespace for templates. Any template which is used by many people should be held there, under "Template:", so for example the reflist template is held under Template:Reflist. If you use curly braces around reflist ({{Reflist}}) the clever wiki software looks at it and relises that it should look in the template namespace.

However, you can over-ride this, by telling it specifically which namespace you want to look in. For example, I could hold a template in my userspace - many users do, however I am not one of them, so here is an example of someone else's: }}User:Worm That Turned/Welcome}}. The markup sees that it should be looking in the User namespace, and goes there.

How do I write templates?[edit]

The basics of templates is just the same as any other page. You can have a text only template, so that the same text can be used on many pages. But that's not where the real power comes in. The real power comes with parameters.

Un-named Parameters[edit]

The most basic parameter is {{{1}}} (note the three curly braces - not two!). When you use {{{1}}} in a template, it will accept the first un-named parameter passed in. Confused? How about an example?

Say I create a template called Template:Magic with the following code.

"This magic trick was first performed by {{{1}}}"

I could call it by putting {{Magic|Adam}} and the output would be

"This magic trick was first performed by Adam"

You can go on to add other un-named parameters {{{2}}},{{{3}}} and so on. And in this case Adam would be used everywhere a {{{1}}} is shown.

Named Parameters[edit]

We also have named parameters. They are used the same way as unnamed parameters, but when called you have to say which parameter you are calling. I have a feeling you're looking confused again. Let's do another example.

Using the same template as I created about, Template:Magic I could change the parameter to {{{name}}}

"This magic trick was first performed by {{{name}}}"

I would then call it by putting {{Magic|name=Adam}} and the output would be

"This magic trick was first performed by Adam"

Useful for when you're calling many different parameters, say on an infobox.

Default values[edit]

Any parameter can have a default value, ie a value if no parameter is passed in. The syntax is {{{1|default value}}}.

Using the same template as I created about, Template:Magic I could add a default value...

"This magic trick was first performed by {{{1|someone very clever}}}"

I would then call it by putting {{Magic}} and the output would be

"This magic trick was first performed by someone very clever"

includeonly and noinclude[edit]

There are two very useful tags that you can use to change how things appear. includeonly tags will only show when the template is placed. noinclude tags will only show on the template page. So, if you want something to change when it's placed, then the includeonly is useful (perhaps a locked timestamp). If you want something on the template page only, then the noinclude is useful (perhaps for template documentation).

Example? Yeah, I thought so. Let's go back to Template:Magic. If the code is (CURRENTTIME is a magic word, which returns the current time when called. Clever that)

"This magic trick was first performed at <includeonly>{{CURRENTTIME}}</includeonly>
<noinclude>the current time" </noinclude>

You could go to Template:Magic and see

"This magic trick was first performed at the current time"

But if you were to call it, you'd get

"This magic trick was first performed at 01:23"

Other tricks[edit]

There's all sorts of other things you can do with templates, but it gets complicated from here on in. Have a look at Help:Magic words, you'll be amazed at what they can do. I'm going to teach you one more thing before I let you pass this module, and that's the #if: function. It's quite simple really - it works in the following format. {{#if: test string | value if non-empty | value if empty }} where it checks if the parameter "test string" is empty.

So... let's try an example. Template:Magic again. I'm beginning to like it.

"This magic trick was first performed by {{{1}}} {{#if:{{{time|}}}|at {{{time}}} | long ago}}"

Here it checks if the parameter {{{time}}} is null, and if it is it changes the text (the reason I've used {{{time|}}} is so that when the parameter isn't passed in, it defaults to nothing. Otherwise it defaults to {{{time}}}, as in the actual text - {{{time}}}, which just gets confusing).

So you could call it by typing {{Magic|Adam}} and you would get

"This magic trick was first performed by Adam long ago"

or you could call it with a time, {{Magic|Adam|time = 4pm}} and you would get

"This magic trick was first performed by Adam at 4pm"

Ta-da, you've just learnt templates!