Search code examples
mediawikimediawiki-templates

MediaWiki template to filter table contents


I have a MediaWiki site hosted on ShoutWiki. I want to create a template that will return a table, with rows filtered by the template's single argument. The table could be stored in whatever format works. It will have three columns, and I want to display rows only if the template's argument is a substring of the text in the first cell in the row. The search needs to be case sensitive.

There are JavaScript solutions for this, but I'd like to do it on the server if possible.


Solution

  • If you don't have a special extension handling that (e.g. Scribunto adding Lua support and therefore a real programming language to MediaWiki), you need to encapsulate each row into an own template call.

    Example:

    Template:FilteredRow

    {{#ifeq:{{{1|}}}|{{{2|}}}|<tr><td>{{{2|empty row}}}</tr></td> }}
    

    Template:A_Table

    <table>
    {{FilteredRow|1={{{filter|}}}|2=some content here}}
    {{FilteredRow|1={{{filter|}}}|2=some content here in row 2}}
    {{FilteredRow|1={{{filter|}}}|2=some content here in row 3}}
    {{FilteredRow|1={{{filter|}}}|2=baz}}
    </table>
    

    Using:

    {{A_Table|filter=baz}}
    

    Result:

    <table>
    <tr><td>baz</td></tr>
    </table>
    

    With Scribunto, you could simply save your table as HTML table, or JSON, or whatever parser you find. Note that JSON support (recognition, formatting, validation) in MediaWiki and user namespace is being worked on.