Use the ask template format to create tabular output

From Semantic MediaWiki Community Wiki
Jump to: navigation, search

Extension: Semantic MediaWiki

Description:

The 'ask' inline-query is a versatile way to harness the power of Semantic MediaWiki on your site [docref]. It supports a number of different output formats, including the tabular output format [docref,docref]. 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 [docref].

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[edit]

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:

  Has city Has country
Adelux Asnières sur Seine France
Ambler LLC Centerville USA
… further results


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[edit]

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

<includeonly>
 | {{{1}}} || {{{2}}} || {{{3}}}
 |-
</includeonly>


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 v0.


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. However, the query will not always work as expected (see this discussion for more information).

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

Header:

<includeonly>
{| class="wikitable sortable"
 ! Organization !! Country !! City
 |-
</includeonly>

Footer:

<includeonly>
|}
</includeonly>

Step 3, combine the ask query with the templates[edit]

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 v0
 | introtemplate=Example table header v0
 | outrotemplate=Example table footer v0
 }}

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 v0').

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


Organization Country City
Adelux France Asnières sur Seine
Ambler LLC USA Centerville
… further results

Step 4, Customize your tabular output[edit]

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 v1 with the following wiki text:

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


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


Organization Country City
Adelux France Asnières sur Seine
Ambler LLC USA Centerville
… further results

Conclusion[edit]

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 [docref]). 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 [docref] to see if someone else has already implemented what you want to do.

Notes[edit]

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

Bugs[edit]

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