Help:Inline queries/Using templates for custom formatting

Subpages of the main Help page:
 * 1 /Introduction to "hash"ask
 * 2 /The "hash"show parser function
 * 3 /Standard parameters for inline queries
 * 3.1 /Result limits and links to further results
 * 3.2 /Introduction and default text
 * 3.3 /Sorting results
 * 3.4 /Configuring labels or table headers
 * 3.5 /Changing the first result column
 * 4 /Result formats
 * 5 /Exporting query results: RSS, etc.
 * 6 /Using templates for custom formatting

Some of the above result formats support the use of wiki template to fully control the display of an inline query. This works for the formats template, list, ol and ul. 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 template, 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 ), so parameter  is the page title, and ,, ...</tt> 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  | link=none | </tt> to disable SMW's automatic linking of article names; your template will then have to add  </tt> 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 format=table</tt> first. For example, queries that refer to a single page only (like the ones one would use with #show</tt>) hide the page title of the result page, so that the parameter </tt> refers to the first printout statement. Using the printout statement ?</tt> or specifying any value for mainlabel</tt> willchange 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:

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 ?Area#km²=Size in km²</tt>, and will similarly respect all given display formats (see Help:Displaying information).

Below is a similar query sorted by population that uses format=ol</tt> to produce a numbered list.

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 |?</tt>

Result:

The same can be accomplished using #show</tt> even though this may not be the most typical use of this function:

Result:

Templates may also include other parser functions such as conditionals and even queries. Examples of complex query formats can be found on the following pages (external links, may change):


 * Upcoming events on semanticweb.org's Main Page: the events section on this page displays only certain major events. Each such event is formatted with a template that uses another inline query to find sub-events (co-located workshops, tutorials, etc.) for a given event.
 * Publications on korrekt.org: all lists on this page are created with templated queries. Conditionals (#if</tt> and #ifeq</tt>) are used to change the format of a result depending on its publication type and provided data (majorpublications have bold-faced titles).