1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Creating and using templates

This page is a chapter in 'Book Encyclopedia Writers Guide'

Templates enable standardized sections of articles to be created.

Using templates


When similar text is used several times in an article or in different articles, it can be easier to create a template for that text. Then whenever the text is needed, the codes
[template]template name|parameters[/template]
are put in the article and these codes will automatically be replaced by the text specified in the template definition.

Templates are useful when:
  • The effort required to simply to create the text in the article is more that the effort required to create and use the template. This will only happen when the text is repeated a number of times.
  • A standardized format is needed and a template is used to ensure every occurrence of the text is in that format.
Templates are especially useful when they are listed in the Templates drop-down menu in the article editing window. Then all the template codes are inserted into the article for you and all you have to do is fill in any parameters, particular information need to generate the correct text.

(At the moment there is a problem in the system and the Templates menu will not automatically appear when you edit an existing article. However, above the edit window there will be the statement This tab was loaded via AJAX. For your browser to attempt caching any edits you make this session, you should Reload This Tab. By clicking on Reload This Tab the screen will be refreshed and the menu will appear.)

Creating templates


Templates must be created in the special Templates namespace of the Encyclopedia Tools.

A template article should consist of:
[comment]explanation of the template and its use[/comment]
template definition
The template definition can consist of any text and any of the codes normally used to write articles. In addition, two special template codes can be used:
  • Parameters: Parameters are names or numbers enclosed in triple braces:
    {{{parameter name}}} or {{{parameter number}}}
    Parameters allow different information to be inserted into the template each time it is used.
  • Conditionals: Conditionals are the special codes:
    [if="test"]true action[else/]false action[/if]
    Conditionals allow the template to generate different information depending on the result of some test, which usually includes parameters.
Because templates must be created in the Templates namespace, the Sandbox cannot be used for them. If you create a template in the Templates namespace to experiment, then please get a moderator to delete it when you have finished with it.

A simple template


The only thing a simple template can do is insert some text in an article.

The template article Template example 1 contains the following:
[comment]This template inserts some formatted text into an article. [/comment]
There are three classes of clock:
  • Long-case clocks which stand on the floor.
  • Shelf clocks which sit on a shelf or mantle-piece.
  • Tower clocks which are large, public clock in buildings or towers.
To use this template, all that is needed is to insert in the article:
[template]Template example 1[/template]
and the following appears:

Such a template is almost useless! Unless the text is to be used several times it will take more work to create and use the template than it would to simply put the text directly in the article.

One situation where a simple template might be useful is in setting up a table; by using a template all articles which need the table will be the same.

For example, it may be useful to have a standardized table for the production of a watch company. Template example 2 contains the following:
[comment]This template creates the header for a watch production table. [/comment]
BeginEndCountDateComment

This template, together with [/table] creates the following table heading:
By adding some lines we get a table which will be the same in appearance in every article that uses the template. So:
[template]Template example 2[/template]
|-
|1,000||1,400||400||1853||First run of 1857 model watches produced by Appleton, Tracy & Co.
|-
|1,401||1,420||20||1854||First of many short runs that alternated balance styles on the basic 15J movement
[/table]
Will produce the table:

|-
|1,000||1,400||400||1853||First run of 1857 model watches produced by Appleton, Tracy & Co.
|-
|1,401||1,420||20||1854||First of many short runs that alternated balance styles on the basic 15J movement
[/table]

Changing templates


Changing a template can cause pandemonium! If a template is used in many articles and you change it, the result may be that most of the articles are now wrong. For example, what happens if you change Template example 2 to have two dates (Template example 3):
[comment]This template creates the header for a watch production table. [/comment]
BeginEndCountFirst dateLast dateComment

Now all articles using the template will automatically change with the following unfortunate result:
1,0001,4004001853First run of 1857 model watches produced by Appleton, Tracy & Co.
1,4011,420201854First of many short runs that alternated balance styles on the basic 15J movement
Changing the appearance of the table may be OK, but changing its contents would be a disaster. For this reason standardized templates are locked.

Before changing a template, look at the template’s article. After the description of the template there is a list of all articles that use that template. If your article is the only one listed then you can change the template without any problems. But if other articles (especially ones you did not write) use the template then you must make sure your changes will not cause any problems with those other articles.

Templates with parameters


Templates with parameters are much more useful. They enable specific information to be inserted in a template.

The above example can be used to illustrate the point. To make the table look nice, the numbers should be right aligned and the date centered. But to do this takes quite a bit of work; we would have to write:
[template]Template example 2[/template]
|-
|align="right"|1,000
|align="right"|1,400
|align="right"|400
|align="center"|1853
|First run of 1857 model watches produced by Appleton, Tracy & Co.
|-
|align="right"|1,401
|align="right"|1,420
|align="right"|20
|align="center"|1854
|First of many short runs that alternated balance styles on the basic 15J movement
[/table]
Now it would be easier if we could get a template to do most of the work.

Numbered parameters


Numbered parameters are the easiest to use, but they must always be in the right order and all (except the last) must be included. Numbered parameters are substituted in the template in the order in which they are supplied.

Using numbered parameters we can vary the width of the table and create a template for the lines in the watch table of example 2.

First, the table header, Example 2, is changed to the following (Template example 4):
[comment]This template creates the header for a watch production table. [/comment]
BeginEndCountDateComment

Now we can vary the width of the table to suit our needs. So
[template]Template example 4|80[/template]
produces:
Second, we can now create a useful template to generate table rows (Template example 5):
[comment] This template produces rows of data for the watch production table. To use it insert the following code into the article:
[template]Template example 5|first number||last number||count||date||comment[/template]
substituting the actual values for the serial numbers, count, date and comment.
Note: this template must be used with Template example 3 which generates the table header.[/comment]

|-
|align="right"|{{{1}}}
|align="right"|{{{2}}}
|align="right"|{{{3}}}
|align="center"|{{{4}}}
|{{{5}}}
So, to generate the entire table we need to include in the article:
[template]Template example 4|80[/template]




[/table]
This produces:





[/table]
The two additional rows show what happens if you the omit numbered parameters for the last number and the count. The correct table will only be generated if these parameters are included but left blank.

Named parameters


Named parameters overcome the above problem. Indeed, not only can parameters be omitted, but their order can be changed.
Templates Template example 6 and Template example 7 are the same templates for the watch production table, but now they use named parameters. Example 6 has one named parameter wid and example 7 has five named parameters first, last, count, date and cmt.
The following example produces the same type of table:
[template]Template example 6|wid=80[/template]



[/table]
The result is:




[/table]
Note that in line 2 of the table the order of parameters has been changed and in line 3 some have been omitted, but the table is correct. (The space in Fir st run of 1857 ... is due to a bug in the system.)
Using named parameters requires more work to create the table and so they would not normally be used in templates like those in these examples.

Conditional templates

Conditional templates enable you to create templates that can cater for different situations. There are three forms:

Default values


The simplest conditional is to provide a default value to be used if a parameter is omitted or is blank.

For example, {{{date|n.d.}}} will insert a date or, if the date is not provided n.d. will be inserted. Another example is {{{cmt|No information}}}

Defaults can also be used to provide warning messages. If a parameter such as {{{first}}} is compulsory then using {{{first|serial number required}}} will insert the message serial number requiredif the parameter was omitted.

if="parameter"


[if="parameter"]action if parameter is not blank[else/]action if parameter is blank[/if]
Although defaults can be used in many situations, sometimes we need more control. A simple example is the Ref book template:
{{{1|[b][i]Author![/b][/i]}}} {{{4|n.d.}}}, {{{2|Title of book!}}}, {{{3|(no publisher)}}}[if="{{{5}}}"] ({{{5}}})[/if].
To use this template we insert the following in an article:
[template]Ref book
|author(s) or company or Anon
|title
|name and place of publisher (or blank if unknown)
|date of publication (or blank if unknown)
|optional additional information such as volume, series title, etc.
[/template]
If we leave every parameter blank the template will produce:

where warning messages and default values have been created for all parts except the optional information.

However, we cannot use a default for the optional information. If there is no information then nothing is displayed. But if there is some information then we need to create (information); note that there has to be a space before the information. This can be done using:
[if="{{{5}}}"] ({{{5}}})[/if]
Here the if= code tests to see if the parameter is blank. If it is not then the space and parentheses are created together with the parameter. Because nothing needs to be done if the parameter is blank, the
[else/]action if parameter is blank
has been omitted, but the code could have been written as [if="{{{5}}}"] ({{{5}}})[else/][/if].

Another example is:
[if="{{{last}}}"]pages {{{first}}} to {{{last}}}[else/]page {{{first}}}[/if]
If first=238 and last=304 the code will generate pages 238 to 304. If last is left blank it will produce page 238.

if="comparison"


[if="comparison"]action if comparison is true[else/]action if comparison is false[/if]
A comparison has the form value1 operator value2 where the values can be parameters and the operator is one of <, >, <=, >= or ==.

These operators basically compare character strings. They do compare numbers as numbers, including signs, but numerical expressions, such as 10-3, are not evaluated and are treated as text strings.

The following table provides some examples of comparisons. The table is generated by:
[table]width="70%" border="1" align="center"
!align="center"|param 1
!align="center"|param 2
!align="center"|<
!align="center"|<=
!align="center"|>
!align="center"|>=
!align="center"|==
[template]Template example 8|1|6[/template]












[/table]
Template example 8 generates one row of the table:
|-
|align="center"|{{{1}}}
|align="center"|{{{2}}}
|align="center"|[if={{{1}}}<{{{2}}}]true[else /]false[/if]
|align="center"|[if={{{1}}}<={{{2}}}]true[else /]false[/if]
|align="center"|[if={{{1}}}>{{{2}}}]true[else /]false[/if]
|align="center"|[if={{{1}}}>={{{2}}}]true[else /]false[/if]
|align="center"|[if={{{1}}}=={{{2}}}]true[else /]false[/if]
param 1param 2<<=>>===












As you can see from this table, character strings are compared from left to right.

This page has been seen 2,082 times.

Current Discussion: Main discussion

  1. No comments have been posted for this discussion.