User:Yunshui/Templates for beginners

You've no doubt seen at least some of the many templates - navboxes, infoboxes, boilerplate warnings, tags, notices and so-one - that are everywhere on Wikipedia. You may even have used some of them yourself. This page is intended to give a brief, informal introduction to templates and how they work; for more information, see this guideline.

What is a template?
No-one wants to spend ages typing up the same piece of information over and over again, yet on Wikipedia, it's often necessary to add generic messages to article or talk pages. For this reason, we have templates which can be used to add commonly-used messages or formats with the minimum number of keystrokes.

Templates work by copying the contents of the template page onto the page where you place the template. This is known as "calling" the template. For example, adding the text  to a talkpage tells the software to go to the page Template:Like and copy the contents to the destination talkpage - giving you this:. Since the markup text of even the comparatively simple Template:Like looks like this: "" you can see why it's preferable to simply call the template, rather than typing out the entire code.

Templates are used for a number of different purposes. Some of the most common types are as follows (a complete list can be found here):
 * Cleanup: added to articles to highlight areas for improvement.
 * Deletion: used to propose articles for deletion in various ways.
 * Warnings: posted on user talk pages to discourage inappropriate editing practices.
 * Navigation: added to articles to help readers find other pages relating to a topic.
 * Infoboxes: added to articles to collate basic information about a subject.

Transclusion or substitution?
There are two methods for calling a template:
 * Transclusion creates a link showing the contents of the template page on the destination page. This means that changes to the template page will show up on the page where it has been used. To transclude a template, enclose its name in curly brackets, thus:.
 * Substitution copies the contents of the template to the destination page, and saves it there. Changes made to the template will not show up on the destination page; effectively, substituting a template is the same as copy-and-pasting the template code. To substitute a template, add  in front of the name, like so:

Some templates, like Proposed deletion must always be substituted in order to work; others, like cleanup, should only ever be transcluded. If you are not sure, go to the template page and check the documentation. A number of bots exist to automatically substitute templates if this has not been done, but it is always preferable to do the work yourself.

Parameters
Many templates can be customised to give tailored information. For example, the uw-vandal template is a warning for vandalism, and produces the following when used (substituted): "Information.svg Hello, and welcome to Wikipedia. Although everyone is welcome to contribute to Wikipedia, at least one of your recent edits did not appear to be constructive and has been reverted or removed. Please use the sandbox for any test edits you would like to make, and read the welcome page to learn more about contributing constructively to this encyclopedia. Thank you." However, if the user being warned has edited several articles, it can be helpful to specify which article has been vandalised. To do this, we add the problem article as an additional parameter, using a pipe:, for example, gives us this message: "Information.svg Hello, and welcome to Wikipedia. Although everyone is welcome to contribute to Wikipedia, at least one of your recent edits, such as the one you made to Sausage, did not appear to be constructive and has been reverted or removed. Please use the sandbox for any test edits you would like to make, and read the welcome page to learn more about contributing constructively to this encyclopedia. Thank you." It is also possible to use multiple parameters, again separated by pipes. The uw-vandal template allows for additional text as a second parameter, so typing this:  gives this: "Information.svg Hello, and welcome to Wikipedia. Although everyone is welcome to contribute to Wikipedia, at least one of your recent edits, such as the one you made to Sausage, did not appear to be constructive and has been reverted or removed. Please use the sandbox for any test edits you would like to make, and read the welcome page to learn more about contributing constructively to this encyclopedia. It is not appropriate to discuss the size of your sausage on Wikipedia." Each template page provides a list of available parameters in its documentation (see Template:Uw-vandal1 for the example above). Some templates use named parameters; for example, the cite book template uses parameters such as,   and. In such cases, place the information to be displayed after the = sign.

Creating templates
Although templates can have quite complex code, they can be created by any user.
 * First, start a new page at Template:Name of your new template. It is possible to create and host templates in userspace (many Userboxes are hosted this way, see User:Yunshui/Userbox Yang style as an example), but for any "official" template, it is preferable to use the Template namespace.
 * Then create the text/image/box that you want to appear when the template is used. You can do this by copying the code of a similar template and changing the appropriate elements, or you can write it from scratch (you'll need to be comfortable working with Wiki markup, though). Use the Show preview button often to check what effect your changes have had.
 * Create a documentation subpage (e.g. Template:Name of your new template/doc). This is a subpage of the template which details how it should be used: what parameters are available, which pages it should be used on and so forth.
 * Transclude the documentation to the template page. Use  tags to prevent the documentation from showing on the destination page when the template is called, thus:.
 * You can now use your template around Wikipedia: call it using.