Using MediaWiki Templates ------------------------- An overview of how to use and create MediaWiki templates. ### What are templates used for? Templates exist primarily to make it easy for a piece of content to be re-used in multiple pages. Some example uses for templates include: - Pre-styled and self-contained "badges" or "banners" that can be placed on multiple pages. - Fill-in-the-blanks charts or tables. - Automatic generation of complex page content based on simple data points. An example of a very simple template of the "banner" variety is , which can be seen on a handful of pages as a common indicator that the project is probably no longer being maintained. A more complex example is , which generates a styled table of data about a project as well as (potentially) category links for the including page based on that data. ### How do templates work? If you have a programming background, it's easy to think of a template as a function -- when you have a piece of program logic you want to be able to reuse in multiple places, you encapsulate it in a function, and you can then invoke it whenever you like. If you need to change that commonly-used bit of logic, you only need to change the function definition; everything that calls the function is affected automatically. If you don't have a programming background, think of a template as a way to copy and paste something into a bunch of pages without having to actually copy and paste it, and at the same time make it possible to update it in all of those pages by editing it in a single place. Another way to think of a template is as a type of link that places its contents directly in the linking page. A simple template is invoked with the following syntax: `{{`**`TemplateName`**`}}` When the page is rendered, the contents of the page **Template:TemplateName** will be inserted in its place. The template page itself can modify what its including pages receive from it with the <includeonly> and <noinclude> tags, which specify that their contents should respectively **only** appear in including pages or **never** appear in including pages. This is used to separate the actual "body" of the template from documentation seen when the **Template:TemplateName** page is viewed directly. Templates can also include parameters, which are included in the template usage like this: `{{`**`TemplateName`**`|`**`param1`**`|`**`param2`**`...}}` The template page has access to these parameters as **{{{1}}}**, **{{{2}}}**, et cetera. If you want to use a default value in cases where no parameter is passed, you can specify this as **{{{1|default}}}**. If no default value is specified in this manner and the parameter is not passed, the literal text **{{{1}}}** will be used as the default value. When invoking a template with parameters, note that if one of the parameter values includes a pipe (**|**) character, it must be escaped. The HTML code **&\#124;** is one way of doing this, although this also prevents the character from being used as part of other wiki markup, such as in a table. Another way (which allows the pipe character to be used in other markup) is to wrap the pipe in its own template; this wiki (and others such as Wikipedia) use for this purpose, so that a pipe character in a template parameter can be expressed as **{{!}}**. Parameters can also be given arbitrary names rather than being numbered by the order in which they appear, which is usually preferable when there are too many of them to keep straight otherwise. The syntax in this case is: `{{`**`TemplateName`**`|`**`var1=val1`**`|`**`var2=val2`**`...}}` The template page will then have access to these as **{{{var1}}}**, et cetera. ### A contrived example Suppose that we create a page called **Template:Trio** and give it these contents: `'''{{{1}}}''', {{{2}}}, and ''{{{3}}}''` Then suppose that in another page we put the text: `{{Trio|Huey|Dewey|Louie}}` This would result in a page containing: **`Huey`**`, Dewey, and `*`Louie`* ### A real-life example The template [Template:Sam Stafford](Template:Sam_Stafford "wikilink") is another example of a simple template that takes no parameters and generates a banner for inclusion on a number of pages. If you click on that link, you'll see exactly what the template produces when it's used on a page, since no special tags are used to modify what is included from that template. Clicking the **What links here** toolbox link from that page shows you all of the pages that use that template; when the template is updated, all of those pages immediately see the change. If you look at the source for this template, which as of this writing is: `{{Contributor|Sam Stafford|`\ `[[AuthP4]] {{!}} [[CALAMARI]] {{!}} [[DeepAnnotate]] {{!}} [[Genesaver]] {{!}} `\ `[[P4HL]] {{!}} [[Monthly Stats]] {{!}} [[p4rollback.pl]] 
`\ `[[Perforce MediaWiki extension]] {{!}} [[Perforce Command Line Recipes|Perforce one-liners]] {{!}} `\ `[[Rollback]] {{!}} [[Scenesaver]] {{!}} [[WebThumb]]}}` you'll notice that it provides the contents of the banner but not the layout. For this, it itself uses another template, . This template's source is: ``\ ``\ `{{{1}}}'s Projects`\ ``\ `{{{2}}}`\ `` This provides the basic styling and layout for the banner, and gets its contents from the two parameters passed to it; the first parameter is the name of the contributor for the top of the banner, and the second parameter is the list of links for the bottom. This allows multiple different contributor-specific banners to be created without each one having to set up the styling and layout. If is ever changed, the change will be reflected in [Template:Sam Stafford](Template:Sam_Stafford "wikilink") and therefore all of the pages that use that template in turn.