WSU Technology Knowledge Base:Adding content templates to articles

From WSU Technology Knowledge Base
Jump to: navigation, search
Wiki Author Series
Author series pic.jpg
This article is part of the

Wiki Author Series

Overview

A template is an article created in the Template: namespace that can be embedded into another article. This process is called transclusion. It's very handy for adding a common block of content to a bunch of articles. This saves time by allowing you to create one copy and reuse it. If you change the contents of the template, the changes are reflected on every page that includes that template. Sometimes, a template is just a bit of text that you reuse frequently. However, it can be more complex. It could be a table that includes text and media. It could also include parameters that authors specify when adding the template to an article. Wikipedia uses templates extensively for such things as the infoboxes that appear in many articles.

The Template: namespace

The Template: namespace is intended for special articles that are to serve as templates and be included in articles in their entirety. This namespace is not searched by default, so none of the articles created in the Template: namespace will appear when the typical user searches the wiki. Other than that, you create articles in the Template namespace exactly as you would on Main:, just precede the title of the page with Template: when creating new pages.

When to use templates

  • You want to include details about D2L in every article about D2L. You create a template article called Template:About D2L and then embed it at the top of each of those pages.
  • You want a common image and a set of links to appear as an infobox on every article that is part of the Student Survival Guide series. You create a template called Template:Student Survival Series and include that in a table cell on corresponding articles.
  • This Wikipedia article about Strand in London includes content in a box in the upper right corner. That content has been embedded into the article using a template.

Embedding content from other articles

The transclusion process extends beyond templates. You can embed content from any article in any namespace into any other article. For example, you could embed content from an article about D2L quizzes into a Featured Article area of the main homepage. Both of these articles are in the Main: namespace. In this case, the quizzes article isn't really a template and you do not want the entire article to be displayed in the Featured Article area. You just want some teaser text that will draw the viewer into the full article. This form of tranclusion is typically controlled using includeonly and noinclude tags on the source article.

Example 1 - Transcluding a simple text-only template

For this example, I first created a template article called Template:Example1. I edited the template article and added the L3 heading Example 1 simple template and the text The heading above and this text are being embedded or transcluded into this article from a template called Example1. I then added the following code to this article...

{{Example1}}

and it resulted in this...

Example 1 simple template

The heading above and this text are being embedded or transcluded into this article from a template called Example1.

Notes

  • I did not need to specify the namespace Template: when transcluding the article Example1. If you don't specify a namespace, it will search the Template: namespace by default.

Example 2 - Transcluding an info box template

For this example, I first created a template article called Template:Author. I edited the template article and set up a small table. I styled the table a bit, right-justified it, and added a table header, an image, and a bit of text. I then returned to this article and added the following code at the very top...

{{Author}}

This resulted in the table being transcluded in the upper right corner of this article. This is an example of a simple information box that could be used to tie various articles in a series together. I will add this template to the top of every article in the Wiki Author Series and then I will probably make the Template:Author table more complex over time. I might add a link to an article that lists all the other articles in the series, for example. These changes will be reflected on all of the articles that transclude the Author template.

Example 3 - Transcluding a template that uses parameters

When you create a template article, you can specify parameters or variables that can be provided by authors when they include that template in another article. Instead of just putting the name of the template in double curly brackets, authors can specify values that are then passed to the template and change the results. You have probably already done this when including an image to an article using the File code, wherein you can specify how wide the image should be on the page, whether it should include a caption, etc. These are all parameters that you specify when including an image. You can do the same thing with templates and the cool thing is that you can define your own parameters when creating the template. Do you want authors to be able to specify table header text or the background color of an info box template? Just add those parameters to the template.

I will have an example of how this applies to template posted soon.

Related Links