Search code examples
mediawikiwikimediawiki-templates

Remove rows in a Wiki template, if variable not used


I created a template on a site using mediawiki, which is this:

{| style="width:250px; font-size: 95%;  border:1px solid #eaecf0; background-color:#f8f9fa; box-shadow: 0 1px 2px 1px rgba(0,0,0,0.02); color:black; margin-left:1em; padding:10px; float:right; clear:right; text-align: left;"
!style="text-align: center; background-color:#ffb555; padding:5px;" colspan="2" | <span style="font-size: 1.2rem;"><strong>{{{name}}}<strong><span>
|-
!colspan="2" | [[File:{{{image}}}|250px]]
|-
! style="vertical-align: top; padding:3px 10px"| Age
| style="vertical-align: top; text-align: right; padding:3px; 10px"| {{{age}}}
|-
! style="vertical-align: top; padding:3px 10px"| Zodiac
| style="vertical-align: top; text-align: right; padding:3px; 10px"| [[File:{{{zodiac}}}.svg|22px]] {{{zodiac}}}
|-
! style="vertical-align: top; padding:3px 10px"| Birthday
| style="vertical-align: top; text-align: right; padding:3px; 10px"| {{{birthday}}}
|-
! style="vertical-align: top; padding:3px 10px"| Game Debut 
| style="vertical-align: top; text-align: right; padding:5px; 10px"| {{{appearance}}}
|-
! style="vertical-align: top; padding:3px 10px"| Created by
| style="vertical-align: top; text-align: right; padding:3px 10px"| {{{created}}}
|-
|}

{| style="width:254px; font-size: 95%;  border:1px solid #eaecf0; background-color:#f8f9fa; box-shadow: 0 1px 2px 1px rgba(0,0,0,0.02); color:black; margin-left:1em; padding:10px; float:right; clear:right; text-align: left;" class="mw-collapsible mw-collapsed"
!style="text-align: center; background-color:#4c88ef; color:#ffffff;" colspan="2" | <span style="font-size: 1.00em;">Information<span>
|-
! style="vertical-align: top; padding:3px 10px" | Status
| style="vertical-align: top; text-align: right; padding:3px 10px"| {{{status}}}
|-
! style="vertical-align: top; padding:3px 10px" | Relatives
| style="vertical-align: top; text-align: right; padding:3px 10px"| {{{relatives}}}
|-
! style="vertical-align: top; padding:3px 10px" | Relationships
| style="vertical-align: top; text-align: right; padding:3px 10px"| {{{relationships}}}
|-
|}

However, the relatives, relationships and status variables are no always used, so I'm trying to set it up so if the var is not used, then not to put that row, and if non of the three vars are used, then not to put the whole information table... but I can't manage to do it


Solution

  • You should use metatemplates to make your infobox.

    Basically, you have one template for the infobox, one for the "Information" box and another one for the cell/infobox line

    Here's a metatemplate for the infobox cell, that is called "Template:Infobox cell":

    ! style="vertical-align: top; padding:3px 10px"| {{{1}}}
    | style="vertical-align: top; text-align: right; padding:3px; 10px"| {{{2}}}
    |-
    

    Here, the 1st parameter is the name of the line, and the 2nd parameter is its value.

    Here's the metatemplate for the "Information" box, that is called "Template:Info":

    {| style="width:254px; font-size: 95%;  border:1px solid #eaecf0; background-color:#f8f9fa; box-shadow: 0 1px 2px 1px rgba(0,0,0,0.02); color:black; margin-left:1em; padding:10px; float:right; clear:right; text-align: left;" class="mw-collapsible mw-collapsed"
    !style="text-align: center; background-color:#4c88ef; color:#ffffff;" colspan="2" | <span style="font-size: 1.00em;">Information<span>
    |-
    {{#if:{{{status|}}}|{{Infobox cell|Status|{{{status}}}}}|}}
    {{#if:{{{relatives|}}}|{{Infobox cell|Relatives|{{{relatives}}}}}|}}
    {{#if:{{{relationships|}}}|{{Infobox cell|Relationships|{{{relationships}}}}}|}}
    |}
    

    And this is your infobox, lets call it "Template:Infobox X" (it has its documentation inside the noinclude tags, the onlyinclude tags are here so the infobox without parameters isn't displayed on the page, be careful with excess spaces that may be addded on the pages where you use the infobox):

    <includeonly>
    {| style="width:250px; font-size: 95%;  border:1px solid #eaecf0; background-color:#f8f9fa; box-shadow: 0 1px 2px 1px rgba(0,0,0,0.02); color:black; margin-left:1em; padding:10px; float:right; clear:right; text-align: left;"
    !style="text-align: center; background-color:#ffb555; padding:5px;" colspan="2" | <span style="font-size: 1.2rem;"><strong>{{{name}}}<strong><span>
    |-
    !colspan="2" | [[File:{{{image}}}|200px]]
    |-
    {{Infobox cell|Age|{{{age|}}}}}
    {{Infobox cell|Zodiac|[[File:{{{zodiac|}}}|22px]]}}
    {{Infobox cell|Birthday|{{{birthday|}}}}}
    {{Infobox cell|Game Debut|{{{appearance|}}}}}
    {{Infobox cell|Created by|{{{created|}}}}}
    |}
    {{#if:{{{status|}}}{{{relatives|}}}{{{relationships|}}}|{{Info}}|}}</includeonly><noinclude>
    {{Infobox X
     | name          = Zorro 
     | image         = Face-zorro.svg
     | age           = 99
     | zodiac        = pisces.svg
     | birthday      = 1/1/99
     | appearance    = 2/2/22
     | created       = John Doe
     | status        = Protector of widows<br/>Defender of orphans
     | relatives     = 
     | relationships = Bernardo
    }}
    {{Infobox X
     | name          = Zorro 
     | image         = Face-zorro.svg
     | age           = 99
     | zodiac        = pisces.svg
     | birthday      = 1/1/99
     | appearance    = 2/2/22
     | created       = John Doe
     | status        = 
     | relatives     = 
     | relationships = 
    }}
    <noinclude>
    

    The {{#if:{{{status|}}}{{{relatives|}}}{{{relationships|}}}|{{Info}}|}} line works like this : the first parameter tests if the named parameters exist, if any of them do, the second parameter is executed (here, it is the display of Template info, the "Information" box), in none exist, the third parameter is executed (here, nothing). More info here: https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions

    I tested this infobox on Wikipedia, maybe you should adjust some spaces and image width, the typical infoboxes are 290 px wide, portraits should be a bit smaller. Usually, the width of an infobox cannot be fixed, as the table gets wider with long text, it's a bit difficult to manage.

    It's important to understand the importance of metatemplates: firstly, they greatly simplify the code of the main templates, and secondly, if you try to put part of the code of a table in a parser function (the #if), it won't be displayed correctly, because certain parts must be at the beginning of a line.