Help:Use the template format to create tabular output

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:

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:

| ||  ||  |-

You should be able to recognize the standard table syntax, as well as the template syntax for variables,. Note, for technical reasons, the surrounding 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:


 * Header

Step 3, combine the ask query with the templates
Putting it all together, the resulting wiki text should look something like this:

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:

| ||  || |-

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

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

Bugs
Sorting by date in the resulting table can be problematic. See:
 * https://phabricator.wikimedia.org/T27768