Semantic templates

User manualEditingSemantic templates

Semantic templates are a method of including the markup that Semantic MediaWiki introduces through MediaWiki templates. This has several advantages:

  • users can specify annotations without learning any new syntax
  • annotations are used consistently, i.e. users do not have to look for the right properties or categories when editing a page
  • infobox-style templates provide data structure, by defining which values belong in which pages
  • infobox-style templates also often provide a nice display for the data

For these reasons, semantic templates are a very popular way of handling semantic annotations. In most wikis, semantic data is stored only via templates - in fact, we would estimate that 99% of all SMW data that currently exists is stored via a template. Even SMW-based extensions meant to make free-form SMW annotations easier, like Extension "Halo", do it within the context of a system in which data is also stored using templates.

Infobox-style semantic templates[edit]

Many MediaWiki sites, SMW-based and otherwise, make use of infobox-style templates to present standard information. For example, Wikipedia articles on cities and villages use a standard template in which editors specify common items of information, such as this (from the article "San Diego, California"):

{{Infobox Settlement
|image_skyline        = Sandiego_skyline_at_night.JPG?
|imagesize            = 
|image_caption        = San Diego Skyline | March 31, 2007
|official_name        = City of San Diego
|settlement_type      = [[City]]
|nickname             = America's Finest City
|motto                = Semper Vigilans ([[Latin]]: Ever Vigilant)
|image_flag           = Flag of San Diego, California.svg

Usually the template (in this case, Infobox Settlement) displays this information in a nicely-formatted table. Such regular templatized items of information are ideal to be mapped into properties in Semantic MediaWiki, with each field being stored using a separate property.

The sample page Demo:California shows a simple "infobox" display template that includes semantic annotations.

Inline semantic templates[edit]

You can also use semantic templates anywhere within a page's text, for special cases. For example, when giving the value for surface area of an astronomical object in a wiki page, you might want it to display as

6.088 × 1018 m²

which you can achieve by writing

 6.088 × 10<sup>18</sup> m²

This is cumbersome to write, so you might develop a "Template:Surface area" for areas so that editors can simply write

 {{Surface area|6.088|18}}

and the template expands to your desired markup.

To achieve this, template "Surface area" is coded as follows:

 [[Surface area::{{{1}}}e{{{2}}} m²|{{{1}}} × 10<sup>{{{2}}}</sup> m²]]

This template handles both storing the value semantically (via the "Surface area" property tag), and displaying it a nicely-formatted way on the screen (via everything that comes after the "|" in the tag).

See the sample page Sol and view its source to see this template in use.

Using semantic templates correctly[edit]

While the above pattern allows you to create all kinds of semantic templates of arbitrary complexity, there are some issues to consider.

Automatic annotation requires strict formats[edit]

You can annotate template fields automatically, but in this case the supplied values must adhere to the expected format. For example, it makes sense to store the population of a city with a property of datatype "Number"Holds integer and decimal numbers, with an optional exponent. Still, the value that users enter might not be a number - it could be a piece of text like "Around 1 million". Wikipedia notably has many such exceptions: in the main infobox template at France, for instance, the entry supplied for population is not a single number, or even a set of numbers! Instead, there are multiple numbers and textual explanations of their meaning. Such special cases should be kept in mind when designing semantic templates.

Optional entries and conditionals[edit]

In the normal course of operating a wiki, most pages will probably not have their infoboxes completely filled, and you might not want to show empty rows in such cases. To remove rows with blank values, you can add conditionals into the template code, which include a row (and its annotation) only if a non-empty value was provided.

This can be achieved with the help of the #if parser function, defined by extension "ParserFunctions" ( Wikipedia contains many examples of parser functions in templates, as for instance in template "Taxobox". Using parser functions typically results in difficult-to-read template code, but the simplification for users can be substantial.

Queries in templates[edit]

Inline queries are sometimes added to templates as well, and they almost always contain the {{PAGENAME}} variable. Such queries are usually used to aggregate a set of pages that have some property pointing to the page in question, such as the template for a country page using a query to show a list of each country's cities.

Annotating a template[edit]

You can also add annotations directly to templates, i.e. assign them semantic properties directly. Though this is not recommended, since templates don't represent a real-world entity, it can be done, if the site administrator enables it.

Editing template calls[edit]

A popular extension, extension "Page Forms"Allows to create and use forms for adding and editing pages with and without semantic data (formerly known as Semantic Forms), lets users add and edit calls to infobox-style templates using forms. See also the list of extensions to Semantic MediaWiki.