Help:Use templates to create tabular output

From Linux Web Expert

File:OOjs UI icon lightbulb-yellow.svg <translate> Note:</translate> The inline-query using #ask is a versatile way to harness the power of Semantic MediaWiki on your site. It supports several 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 using the "plainlist" 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? Let's 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:


We build the query using the 'format=table' output format. We initially wanted this, 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:

<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! Let's name this template "Template:TableBody." Also, ensure there are line breaks before and after, as shown.

Above, you will see that the template formats the rows of the table. This means we still need to start and end the table and define the column headers. The easiest way to do this is 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>

Let's name this template "Template:TableHeader". Ensure there are line breaks before and after the tags, as shown.

Footer
<includeonly>
|}</includeonly>

Note that you have to keep the closing </includeonly> in the same line to avoid an extra break being added after the template. Let's name this template "Template:TableFooter". Ensure that there is a line break before the tag, as shown.

Step 3, combine the ask query with the templates

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:


Step 4, Customize your tabular output

Now that this works, you can edit how 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}}} |: |-1 }}|{{#explode: {{{1}}} |: |-1 }}]] || 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 the 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:


Conclusion

Although relatively trivial, the above example 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 several ways, especially for 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"No description was provided. to see if someone else has already implemented what you want to do.

Issue

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

Note

Prior to Semantic MediaWiki 3.0.0Released on an unknown date unknown versions of MediaWiki 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"No description was provided.. The name "template" is, however, still an alias to "plainlist" to avoid breakages when upgrading from earlier versions of Semantic MediaWiki.

See also

#scite could not render a citation text for reference "mw:phab:T27768" because type "issue" was not assigned to a template.

References

  1. ^ mw:phab:T27768