User:Bmusician/Adoption/Assignments/Templates

=== Seventh Assignment: Templates ===

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. 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: 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. 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". 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. 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.

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:. 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:. 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 tags. See below.

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.

Discussion
Any questions or would you like to take the test?