Help:Using templates

From semantic-mediawiki.org
(Redirected from Help:Using templates)
Table of Contents

This help page describes the template parameter that is available in many result formats most notably result format "Plainlist"Outputs results in a comma separated list, with additional outputs shown in parentheses. The output does not provide class attributes to HTML elements (default for queries without printout statements).. The parameter is used to specify a template used to display the output of a query.

In general templates are used when printouts require more complex handling for an output than just displaying them with result format "Table"Outputs the results in a table (default for queries with printout statements). and it's more basic display options. Moreover it is much easier to reuse the templates for different queries.

Until Semantic MediaWiki 3.0.0Released on 11 October 2018 and compatible with MW 1.27.0 - 1.31.x. there used to be result format "Template"Uses a specified template to format and display the results which has been deprecated1 and was superseded by result format "Plainlist"Outputs results in a comma separated list, with additional outputs shown in parentheses. The output does not provide class attributes to HTML elements (default for queries without printout statements)..

Result formats[edit]

Some result formats support the use of templates to control the display of the output of a query.

Provided by Semantic MediaWiki[edit]

  • list – Outputs results in a comma separated list, with additional outputs shown in parentheses. The output introduces class attributes to HTML elements to facilitate easy individual styling.
  • ol – Outputs results as a numbered list, with additional outputs shown in parentheses.
  • plainlist – Outputs results in a comma separated list, with additional outputs shown in parentheses. The output does not provide class attributes to HTML elements (default for queries without printout statements).
  • templatefile – Outputs results in arbitrary file formats defined via templates.
  • ul – Outputs results as a bulleted list, with additional outputs shown in parentheses.

Provided by extensions to Semantic MediaWiki[edit]

  • eventline – Displays query results in interactive eventlines
  • listwidget – Outputs results with a navigation or pagination widget for lists (ordered and unordered)
  • moderntimeline – Displays query results in modern interactive timelines
  • oltree – Displays query results as a tree (nested HTML ordered lists)
  • outline – Lists pages in a hierarchical outline, based on property values
  • slideshow – Displays query results as a slideshow
  • tagcloud – Displays queried data in a tagcloud
  • timeline – Displays query results in interactive timelines
  • tree – Displays query results as a tree (nested HTML bulleted lists)
  • valuerank – Lists values based on their occurrence

Usage[edit]

Parameters[edit]

The result formats provide a list of parameters that interact with the usage of templates to display the output of a query. The most important parameters for template usage are listed in the following table:

Parameter Type Comment
template text defines to which template the results will be passed on (no need to use Template: before the name)
introtemplate text (optional) name of a template that is called once before the output of the results (e. g. to define a table header)
outrotemplate text (optional) name of a template that is called once after the output of the results (e. g. to define wiki markup for closing a table)
named args boolean If not set or no, will pass on the arguments to the template without names. If set to yes, will pass on the arguments with names.
userparam text A value passed into each template call.
Not all result formats provide all parameters listed above. See the help pages for the respective result formats for available parameters.

Introduction[edit]

If a template is specified, all result «rows» are formatted using this template. The name of the template (without the initial «Template:») is specified with the template parameter. Thus the query has the following general form:

{{#ask:
 <query-condition>
 |format=<format-name>
 |template=<template-name> 
}}

For each result in an inline query, Semantic MediaWiki 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 {{{1}}}, {{{2}}}, 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 on how to change this), so parameter {{{1}}} is the page title, and {{{2}}}, {{{3}}}, ... are the other properties displayed by the query.

The template feature allows greater flexibility in the reuse and 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 link parameter to "none" ( |link=none ) to disable Semantic MediaWiki'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.

Examples[edit]

Basic example[edit]

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]]
 [[Area::+]]
 [[Population::+]] 
 |?Population
 |?Area
 |format=plainlist
 |template=TableBasicExample
}}
Result
  • Berlin − 3,520,061 on 891.85 km²344.34 sqmi <br />
  • Cologne − 1,080,394 on 405.02 km²156.38 sqmi <br />
  • Frankfurt − 679,664 on 248.31 km²95.87 sqmi <br />
  • Munich − 1,353,186 on 310.43 km²119.86 sqmi <br />
  • Stuttgart − 606,588 on 207.35 km²80.06 sqmi <br />
  • Würzburg − 126,635 on 87.63 km²33.83 sqmi <br />

The template used to create this example: TableBasicExample.

Advanced example[edit]

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]]
 [[Area::+]]
 [[Population::+]] 
 |?=city 
 |?Population=population
 |?Area=area
 |format=plainlist
 |introtemplate=TableAdvancedExampleHeader
 |template=TableAdvancedExampleBody
 |outrotemplate=TableAdvancedExampleFooter
 |limit=4
 |offset=1
 |searchlabel=
 |named args=yes
}}
Result
City Population Area
Cologne 1,080,394 405.02 km²156.38 sqmi <br />
Frankfurt 679,664 248.31 km²95.87 sqmi <br />
Munich 1,353,186 310.43 km²119.86 sqmi <br />
Stuttgart 606,588 207.35 km²80.06 sqmi <br />
Average 929,958 292.78 km²
Total 3,719,832 1,171.11 km²

The templates used to create this example: TableAdvancedExampleHeader, TableAdvancedExampleBody and TableAdvancedExampleFooter.

More examples[edit]

See the help page on result format "Plainlist"Outputs results in a comma separated list, with additional outputs shown in parentheses. The output does not provide class attributes to HTML elements (default for queries without printout statements). for individual examples to the respective parameters provided.

Tips[edit]

Limitations[edit]

The template may execute another ask query. By default the number of nested calls with parameter template is, however, limited to 2 (see the help page on configuration parameter $maxRecursionDepthSets the recursion depth for a template output). 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 a template i.e. "|template=".

See also[edit]


References

  1. ^  Semantic MediaWiki: GitHub issue gh:smw:2488