Help:Use templates to create tabular output

From semantic-mediawiki.org
(Redirected from Help:Use templates to create tabular output)
Help:Use templates to create tabular outputUse templates to create tabular output/fr
Semantic extension(s): Semantic MediaWiki
Further extension(s):  -/-
Keyword(s): query · result format · table · template · plainlist

Description:

The inline-query using #ask 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 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)..

This tip explains how to use the plainlist 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:City]]
 [[Located in::Germany]]
 |?Population
 |?Area
 |format=table
}}

and the results should look similar to this:

 PopulationThe number of inhabitants of some geographical place.Has area
Berlin3,520,061891.85 km²344.34 sqmi <br />
Cologne1,080,394405.02 km²156.38 sqmi <br />
Frankfurt679,664248.31 km²95.87 sqmi <br />
Munich1,353,186310.43 km²119.86 sqmi <br />
Stuttgart606,588207.35 km²80.06 sqmi <br />
Würzburg126,63587.63 km²33.83 sqmi <br />

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:TableBody".

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
<includeonly>
{| class="wikitable"
 ! City !! Population !! Area
 |-
</includeonly>

Lets name this template as "Template:TableHeader".

Footer
<includeonly>
|}</includeonly>

Note that you have to keep the closing </includeonly> in the same line to avoid an extra break to be added after the template. Lets name this template as "Template:TableFooter".

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:City]]
 [[Located in::Germany]]
 |?Population
 |?Area
 |format=plainlist
 |introtemplate=TableHeader
 |template=TableBody
 |outrotemplate=TableFooter
}}

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.

Do not forget to change the output format of the query to 'format=plainlist', and to specify the appropriate template for generating the output ('template=TableBody').

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

City Population Area
Berlin 3,520,061 891.85 km²344.34 sqmi <br />
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 />
Würzburg 126,635 87.63 km²33.83 sqmi <br />

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

<includeonly>
{| class="smwtable-clean sortable"
 ! City !! Population !! Area
 |-
</includeonly>

and a template called "Template:TableBodyCustomized" with the following wiki text:

<includeonly>
 | [[wikipedia:{{#explode: {{{1}}} |; |2 }}|{{#explode: {{{1}}} |; |2 }}]] || style="text-align: right" | {{{2}}} || style="text-align: right" | {{{3}}}
 |-
</includeonly>

Note that we have to use the #explode parser function to get the plain names here since on this wiki the pages are stored in namespace "Demo". Usually [[wikipedia:{{{1}}}|{{{1}}}]] should be sufficient to get the desired result.

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

{{#ask:
 [[Category:City]]
 [[Located in::Germany]]
 |?Population
 |?Area
 |format=plainlist
 |introtemplate=TableHeaderCustomized
 |template=TableBodyCustomized
 |outrotemplate=TableFooter
 |link=none
}}

Note that you need to use the "link=none" setting to suppress property-links to properties of datatype "Page"Holds names of wiki pages, and displays them as a link.

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

City Population Area
Berlin 3,520,061 891.85 km²
Cologne 1,080,394 405.02 km²
Frankfurt 679,664 248.31 km²
Munich 1,353,186 310.43 km²
Stuttgart 606,588 207.35 km²
Würzburg 126,635 87.63 km²

Conclusion[edit]

The above example, although rather trivial, shows the basics of using the 'format=plainlist' 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 do not forget to look at the excellent extension "Semantic Result Formats"Provides additional formats for semantic queries to see if someone else has already implemented what you want to do.

Issue[edit]

Sorting by date in the resulting table can be problematic.1

Note[edit]

Prior to Semantic MediaWiki 3.0.0Released on 11 October 2018 and compatible with MW 1.27.0 - 1.31.x. 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). used to be result format "Template"Uses a specified template to format and display the results. The name "template" is however still an alias to "plainlist" to avoid breakages when upgrading from earlier versions of Semantic MediaWiki.

See also[edit]

References

  1. ^  Semantic MediaWiki: Phabricator task mw:phab:T27768