Search code examples
htmlcssmediawikimediawiki-templates

MediaWiki template table conditional columns


I have a template in my MediaWiki to display information in a tabular manner. The template looks like this (shortened for better overview):

{| align="center" border="1" cellpadding="1" cellspacing="1" style="width: 400px"
| colspan="2" style="text-align: center; background-color: {{{Fon1}}}; color: {{{Fon2}}}"|'''Data'''
|-
| colspan="2" style="background-color: {{{Fon1}}}; color: {{{Fon2}}}"|<center>'''Overview'''</center>
|-
{{#if:{{{Soft Skills|}}}|
{{!}} '''Soft Skills'''
{{!}} {{{Soft Skills|}}}
}}
|-
{{#if:{{{Picture|}}}|
{{!}} {{{ Picture|}}}
|-
|}

They will be filled with an unordered list (<ul><li>Skill One</li><li>Skill Two</li> ... </ul>). There can sometimes be a lot, so I would like them to take two columns when they have more than5 of them.

I have looked at conditional tables, but haven't found anything that helped me. I have also searched no stackoverflow, but didn't find anything.

Can this somehow be achieved?


Solution

  • Splitting lists into columns is fairly easy. There are several templates on Wikipedia that do this, the most popular being Template:Div col, which uses CSS columns (the CSS is kept in MediaWiki:Common.css, which you would need to copy along with all of div col's subtemplates). You might also want to investigate the other column templates to see how they work.

    The hard part is detecting whether the lists have five or more items in them. The template only sees the contents of the {{{Soft Skills|}}} parameter, so you would have to parse that somehow to find the number of items. This is tricky, because you can have both MediaWiki-style unordered lists:

    * Item 1
    * Item 2
    * Item 3
    

    As well as HTML-style lists:

    <ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    </ul>
    

    Also, the HTML-style lists can contain attributes and have inconsistent whitespace, etc.

    Instead, you could restructure your template to accept several Soft Skills parameters, like {{{Soft Skill 1}}}, {{{Soft Skill 2}}}, {{{Soft Skill 3}}}, etc. Then you could do something like this:

    {{#if: {{{Soft Skill 5|}}}
     | {{div col||10em}}
       {{unbulleted list
        |{{{Soft Skill 1|}}}
        |{{{Soft Skill 2|}}}
        |{{{Soft Skill 3|}}}
        |{{{Soft Skill 4|}}}
        |{{{Soft Skill 5|}}}
        |{{{Soft Skill 6|}}}
        |<!-- Add as many more parameters here as you think you will need... -->
       }}
       {{div col end}}
     | {{unbulleted list
        |{{{Soft Skill 1|}}}
        |{{{Soft Skill 2|}}}
        |{{{Soft Skill 3|}}}
        |{{{Soft Skill 4|}}}
       }}
    }}
    

    However, for this you need Template:Unbulleted list, and of course, it is very ugly code.

    To improve this, you can use the Scribunto extension, and write the template in Lua. In fact, constructs like the one above were pretty much the reason that Scribunto was introduced to Wikimedia wikis. (And Template:Bulleted list requires Scribunto anyway.) With Scribunto you can just write a for loop to loop over all the Soft Skills parameters, rather than having to roll out the loop and only be able to iterate a finite number of times.

    If you really want to stick with one {{{Soft Skills}}} parameter, then you can also try to use Scribunto to parse its content, but this will be difficult for the reasons I mentioned above, and because of other corner cases you might run into when parsing wikitext. Using parameters like {{{Soft Skill 1}}}, {{{Soft Skill 2}}}, etc. is the more reliable choice.