Search code examples
templatesmediawikitablelayoutsemantic-mediawiki

Formatting a Semantic MediaWiki query resulttable template with cell newlines


I need a semantic query result table template where I can have both newline-delimited cell results for some columns and comma-delimited columns in the same table.

If I use the standard format=broadtable, for example, the result is separated by newlines inside the table cells for all columns:

{{#ask:[[Category:Items]]
|?Description
|?Models
|?Addons
|format=broadtable
}}

If I create a template, all I am able to accomplish is a comma-separated result:

<includeonly>
{| class="wikitable sortable"
 ! style="width: 30%;" | Page
 !! style="width: 30%;" | Description
 !! style="width: 20%;" | Models
 !! style="width: 20%;" | Addons
 |-
</includeonly>

{{#ask:[[Category:Items]]
|?Description
|?Models
|?Addons
|format=template
|template=QResTemplate
}}

<includeonly>
|}
</includeonly>

Here is the QResTemplate:

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

Every Item has multiple Models and Addons, so it is column 3 and 4 in the table that I need one of them to be comma-separated while the other is newline-separated.

If I add something after {{{3}}}, like a newline, it is added after the last Model on the table row, not after each Model as I want it to.


Solution

  • Use Extension:Arrays to format your comma separated outputs as you want:

    <includeonly><!--
    
           store arrays
    
     -->{{#arraydefine:models|{{{3}}}}}<!--
     -->{{#arraydefine:addons|{{{4}}}}}<!--
    
           print row
    
     -->
     | {{{1}}} || {{{2}}} || {{#arrayprint:models|<br/>}} || {{#arrayprint:addons|, }}
     |-
    </includeonly>
    

    First you store both lists as arrays. arraydefine assumes that your list is comma separated, unless you specify somthing else. Then you print your arrays again with #arrayprint, but this time you can decide how you want those values to be separated.

    If you can't use commas (e.g. because some value contains a comma, you can add e.g. sep=¤ to your ask query, and then do {{#arraydefine:models|{{{3}}}|¤}} to tell arraydefine that you are using a different separator.