Help:Template format

The result format template is used to format the results via a template.

Using templates for custom formatting
Some result formats support the use of wiki template to fully control the display of an inline query. This works for the formats,  ,   and. If a template is specified, all result «rows» are formatted using this template. The name of the template (without the initial «Template:») is given in the parameter, so the query has the following general form:

For each result in an inline query, SMW then calls the specified template, where the result and printout values are used as numbered template parameters. So a query that would display three columns when formatted as a table, will set three template parameters. These values can then be used in the template in the normal way writing,  , etc. Each parameter refers to one "field" or column in the results that would be displayed by the inline query for each selected page. Normally the first field a query displays is the page title (see here), so parameter  is the page title, and   are the other properties displayed by the query. A number of examples are given below.

The template feature allows greater flexibility in the display of the query, including:
 * Changing the order in which output is displayed, or omitting or duplicating output;
 * Displaying images depending on query results;
 * Creating links for property values;
 * Using CSS styles to vary font-size, alignment, background-color, etc. by column in tables.

If you do use a template to adjust the appearance of links, you will probably need to set the parameter  to disable SMW's automatic linking of article names. A similar effect can also be achieved by selecting the plain output format for some or all of the printouts, as described in Displaying information. Your template will then have to add  around anything you want to be a link.

To understand how to create a template for formatting some query, it is useful to look at the query with  first. For example, queries that refer to a single page only (like the ones one would use with ) hide the page title of the result page, so that the parameter   refers to the first printout statement. Using the printout statement  or specifying any value for   will change this.

The following examples all use Template:Query output demo that basically contains the following wiki text:

people squeeze into the of.

The following queries illustrate the effect of this template:


 * Query syntax


 * Query result

In the above example you can see how the template ignores any header labels specified in the query such as «Size in km²». Yet the values the template displays do use the units specified in, and will similarly respect all given display formats (see Help:Displaying information).

Below is a similar query sorted by population that uses  to produce a numbered list:


 * Query syntax


 * Query result

If we directly specify a single page, then normally the query results do not include the page, so to reuse the same template in the query below we must tell the query to display the page title as the first column by adding :


 * Query syntax


 * Query result

The same can be accomplished using parser function #show even though this may not be the most typical use of it:


 * Query syntax


 * Query result

Templates may also include other parser functions such as conditionals and even queries. Example of complex query formats can be found on the following pages (external links, may change):
 * The publications on korrekt.org : all lists on this page are created with templated queries. Conditionals ( and  ) are used to change the format of a result depending on its publication type and provided data (major publications have bold-faced titles).

Usage for (user parameters)
It is also possible to add a (single) extra parameter to the query. See the help page on the  parameter for information on how to do this.

Usage for (named arguments)
For Help:Special:Ask there is the option  (where   corresponds to the alias Inline query  option  ) that specifies that the formatting template expects named arguments, instead of anonymous (numerical) ones.

All examples above populate template parameters like,  , ... but when the  is used, the  template parameters populated depend on its value:


 *   or  : populates parameters like  or
 *  : populates parameters without the  character, like   or.

If you specify an alternative label for the property being requested you can use that as template parameter. But the question mark is still required in the  mode.


 * Notes
 * 1) To access  using   you must name the default output (page name aka ) with: |?#=somename or |?=somename
 * 2) If clicking on the "further results" yields an empty query because the   parameter is not reproduced here, you may have to remove the   parameter.
 * 3) Parameter names are case-sensitive, as always.

Usage of (full parse)
added the  optionCiteRef::gh:smw:1257 which is by default set to. You have to change it to  when you use templates to create an invert annotation or if you want to import (reuse) annotations depending on the result of your query, e.g. when using a variable with them.CiteRef::sb:smw:1255:0CiteRef::sb:smw:1255:1 In short: mainly for annotations to be conditional when inline queries are involved.

Usage of (value separator)
added the  optionCiteRef::gh:smw:2331 when   feature is enabled (default) to avoid ambiguity of the   parameter semantics. The  option separates several values queried from the property annotated to a subject (pages, subojects) while   separates values queried from the same property annotated to different subjects (pages, subojects)CiteRef::sb:smw:2331. Starting with the default value for the   option was changed from a space to a comma.CiteRef::gh:smw:2522 See also the related.

Display of the link to further results
When using the 'template' parameter, you will only see the 'further results...' link when 'format=template'. i.e. it doesn't seem to work with 'format=ol', for example.

If you are creating a tabular output with your template you may need to use the special introtemplate and outrotemplate parameters to ensure that 'further results...' works correctly.

Limitations
The template may execute another ask query. By default the number of nested calls with "format=template" is, however, limited to 2 (see the help page on ). Example: page A contains an inline query that uses template B, which itself contains an inline query that uses template C. Template C may contain yet another ask query, but not one using the template format i.e. "format=template".

Related tips

 * Use ask template format to create tabular output