User:Sumsum2010/Adoption/Lessons/Template Beginner

Templates for Dummies (and you're not a dummy, so it should help)
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. ,,  and  are very common ones, along with templated warnings.

What is a template?
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, ) or "substituting" it (putting it in curly braces, with the key word subst  ). 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?
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 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 - indeed I do at. The markup sees that it should be looking in the User namespace, and goes there.

How do I write templates?
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
The most basic parameter is (note the three curly braces - not two!). When you use 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 "

I could call it by putting and the output would be

"This magic trick was first performed by Worm"

You can go on to add other un-named parameters, and so on. And in this case Worm would be used everywhere a is shown.

Named Parameters
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 "This magic trick was first performed by "

I would then call it by putting and the output would be

"This magic trick was first performed by Worm"

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

Default values
Any parameter can have a default value, ie a value if no parameter is passed in. The syntax is.

Using the same template as I created about, Template:Magic I could add a default value... "This magic trick was first performed by "

I would then call it by putting and the output would be

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

includeonly and noinclude
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  the current time"

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 "

Other tricks
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. 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 "

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

So you could call it by typing and you would get

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

or you could call it with a time, and you would get

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

Ta-da, you've just learnt templates!