Help:Use the template format to create tabular output

Jump to: navigation, search

Semantic extension(s): Semantic MediaWiki

Further extension(s): -/-

Keyword(s): query, result format, table, template


The 'ask' inline-query is a versatile way to harness the power of Semantic MediaWiki on your site. It supports a number of different output formats, including the tabular output format. However, the tabular format can be quite restrictive. For example you may wish to use the values of the properties returned by the query to construct arbitrary links or wiki text on each row of the table. i.e. you may want to include a small image on each row of the table, or use the value in another query. To achieve this greater level of control over the resulting table, you need to use the 'template' output format.

This tip explains how to use the template output format to get a basic tabular output. You should be able to customize this example to meet your specific needs.

Step 1, Create your ask query as normal

You should already know how to do that right? Lets assume your ask query looks like this:

{{#ask: [[Category:Organizations]]
 | ?Has country
 | ?Has city
 | format=table 

and the results should look similar to this:

Note that for developing the query we use the 'format=table' output format. This is what we want initially, but we will be customizing this shortly.

Step 2, create the template to format the table

The template will render the rows of the table (using wiki text), so it should look something like this:

 | {{{1}}} || {{{2}}} || {{{3}}}

You should be able to recognize the standard table syntax, as well as the template syntax for variables, {{{n}}}. Note, for technical reasons, the surrounding <includeonly> tags are required for the template to work correctly! Lets name this template as "Template:Example table layout".

Above you will see that the template just formats the rows of the table. This means that we still need to start and end the table, and define the column headers. The easiest way to do this is just to type the appropriate table syntax before and after the ask query.

The safest approach is to create a header template and a footer template to start and end the table:

{| class="wikitable sortable"
 ! Organization !! Country !! City

Step 3, combine the ask query with the templates

Putting it all together, the resulting wiki text should look something like this:

{{#ask: [[Category:Organizations]]
 | ?Has country
 | ?Has city
 | format=template
 | template=Example table layout
 | introtemplate=Example table header
 | outrotemplate=Example table footer

Note that introtemplate and outrotemplate just put the appropriate wiki table syntax before and after the results of the ask query. The ask query is responsible for generating the wiki text for the rows of the table.

Don't forget to change the output format of the query to 'format=template', and to specify the appropriate template for generating the output ('template=Example table layout').

If you did everything correctly, the result should look something like this:

Step 4, Customize your tabular output

Now that this is working, you can edit the way the table rows are rendered. For example, lets create a template called "Template:Example table layout" with the following wiki text:

 | {{{1}}} || [[wikipedia:{{{2}}}|{{{2}}}]] || [[wikipedia:{{{3}}}|{{{3}}}]]

Replace this template name in the ask query ('template=Example table layout'), and we should have something like the following:


The above example, although rather trivial, shows the basics of using the 'format=template' to get tabular output. Some very basic customization of the tabular output is shown. This example could (and should) be improved in a number of ways, especially with respect to elegantly handling missing values (see parser functions). With these tools in hand, you should be able to create any number of complex tables to display your results, but don't forget to look at the excellent 'results format' extension to see if someone else has already implemented what you want to do.


  • Use the "link=none" setting to suppress 'property-links' to properties of type Page.


Sorting by date in the resulting table can be problematic. See:

This documentation page applies to all SMW versions from 0.7 to the most current version.
      Other languages: fr

Help:Use_the_template_format_to_create_tabular_output en 0.7