Mediawiki display table row only if value is present

301 Views Asked by At

I am the admin of a wiki an currently working on an infobox template. It is made up of a simple table with some variables. Nothing special.

The problem is, that I would like to have an if statment so that only the table rows ar visible where the variable has a value. I have looked in to existing templates on other wikis but can't figure out how to adapt the code.

The following is the code for my template:

{|class="wikitable" style="float:right; margin-left: 10px; border: #94bf88 thin solid;"
|+
! colspan="2" style="background-color: #d9ffda; font-weight: bold; font-size: 20px; color: black; border: none;"  |Artikelinformationen
|-
| colspan="2" style="border: none" |{{{artikelthumbnail}}}
|-
|-
| colspan="2" style= "text-align: center; border: none; font-size: 15px;"|'''Basisdaten'''
|-
|style="border: none"|'''Artikeltitel:'''
| style="width:200px; border: none;" |{{{artikeltitel}}}
|-
|style="border: none"|'''Veröffentlichung:'''
| style="width:200px; border: none;" |{{{veröffentlichungsdatum}}}
|-
|style="border: none"|'''Herausgeber:'''
| style="width:200px; border: none;" |{{{herausgeber}}}
|-
|style="border: none"|'''Art:'''
| style="width:200px; border: none;" |{{{herausgeberart}}}
|-
|style="border: none"|'''Mediengruppe:'''
| style="width:200px; border: none;" |{{{mediengruppe}}}
|-
|style="border: none"|'''Autoren:'''
| style="width:200px; border: none;" |{{{autoren}}}
|-
| colspan="2" style= " text-align: center; border: none; font-size: 15px;"|'''Details'''
|-
|style="border: none"|'''Absätze:'''
| style="width:200px; border: none;" |{{{absätze}}}
|-
|style="border: none"|'''Sätze:'''
| style="width:200px; border: none;" |{{{sätze}}}
|-
|style="border: none"|'''Wörter:'''
| style="width:200px; border: none;" |{{{wörter}}}
|-
|style="border: none"|'''Zeichen:'''
| style="width:200px; border: none;" |{{{zeichen}}}
|-
| colspan="2" style= " text-align: center; border: none; font-size: 15px;"|'''Links'''
|-
|style="border: none"|'''Herausgeber:'''
| style="width:200px; border: none;" |{{{link_heruasgeber}}}
|-
|style="border: none"|'''Artikel in PDF:'''
| style="width:200px; border: none;" |{{{link_pdf}}}
|-
|style="border: none"|'''Artikel im Archiv:'''
| style="width:200px; border: none;" |{{{link_archiv}}}
|}

So for expample: if the variable {{{artikeltitel}}} is empty, both

|style="border: none"|'''Artikeltitel:'''
| style="width:200px; border: none;" |{{{artikeltitel}}}
|-

are not visable.

Only if {{{artikeltitel}}} is given a value, the line shows up when importing the Template into an article.

Thanks in advance.

2

There are 2 best solutions below

4
Robis Koopmans On

You need to put the if statement on the preceding row.

Try this

{|class="wikitable" style="float:right; margin-left: 10px; border: #94bf88 thin solid;"
|+
! colspan="2" style="background-color: #d9ffda; font-weight: bold; font-size: 20px; color: black; border: none;"  |Artikelinformationen
|-
| colspan="2" style="border: none" |{{{artikelthumbnail}}}
|-
|-
| colspan="2" style= "text-align: center; border: none; font-size: 15px;"|'''Basisdaten''' {{#if: {{{artikeltitel}}}|
|-
|style="border: none"|'''Artikeltitel:'''
| style="width:200px; border: none;" |{{{artikeltitel}}} }}
|-
|style="border: none"|'''Veröffentlichung:'''
| style="width:200px; border: none;" |{{{veröffentlichungsdatum}}}
|-
|style="border: none"|'''Herausgeber:'''
| style="width:200px; border: none;" |{{{herausgeber}}}
|-
|style="border: none"|'''Art:'''
| style="width:200px; border: none;" |{{{herausgeberart}}}
|-
|style="border: none"|'''Mediengruppe:'''
| style="width:200px; border: none;" |{{{mediengruppe}}}
|-
|style="border: none"|'''Autoren:'''
| style="width:200px; border: none;" |{{{autoren}}}
|-
| colspan="2" style= " text-align: center; border: none; font-size: 15px;"|'''Details'''
|-
|style="border: none"|'''Absätze:'''
| style="width:200px; border: none;" |{{{absätze}}}
|-
|style="border: none"|'''Sätze:'''
| style="width:200px; border: none;" |{{{sätze}}}
|-
|style="border: none"|'''Wörter:'''
| style="width:200px; border: none;" |{{{wörter}}}
|-
|style="border: none"|'''Zeichen:'''
| style="width:200px; border: none;" |{{{zeichen}}}
|-
| colspan="2" style= " text-align: center; border: none; font-size: 15px;"|'''Links'''
|-
|style="border: none"|'''Herausgeber:'''
| style="width:200px; border: none;" |{{{link_heruasgeber}}} 
|-
|style="border: none"|'''Artikel in PDF:'''
| style="width:200px; border: none;" |{{{link_pdf}}}
|- 
|style="border: none"|'''Artikel im Archiv:'''
| style="width:200px; border: none;" |{{{link_archiv}}}
|}

isolated example:

| colspan="2" style= "text-align: center; border: none; font-size: 15px;"|'''Basisdaten''' {{#if: {{{artikeltitel}}}|
|-
|style="border: none"|'''Artikeltitel:'''
| style="width:200px; border: none;" |{{{artikeltitel}}} }}
0
Noam On

Using conditionals in Wikitext in conjunction with table markup is tricky, for two reasons:

  1. The character | has a syntactical meaning in parser functions (as well as on template calls), interferring with the syntactical meaning of the same character in the Wikitext table markup. For this issue, one workaround is to replace occourances of | that are part of the table markup with the magic word {{!}}.
  2. Parser functions strip out leading and trailing spaces of their output, including linebreaks, while the table markup relies on linebreaks. For this issue, one workaround is to insert a self-closing <nowiki /> tag at the beginning of the function's output, which has no visible effect by itself but makes the linebreak after it to not be ignored.

Here is the code, shortened to only include a few rows of the table. The basis of the code is from @Robis Koopmans's answer, just fixed to address the aformentioned issues and other little issues.

{|class="wikitable" style="float:right; margin-left: 10px; border: #94bf88 thin solid;"
|+
! colspan="2" style="background-color: #d9ffda; font-weight: bold; font-size: 20px; color: black; border: none;"  |Artikelinformationen {{
#if: {{{artikelthumbnail|}}}|<nowiki />
{{!}}-
{{!}} colspan="2" style="border: none" {{!}} {{{artikelthumbnail}}}
}}
|-
|-
| colspan="2" style= "text-align: center; border: none; font-size: 15px;"|'''Basisdaten''' {{
#if: {{{artikeltitel|}}}|<nowiki />
{{!}}-
{{!}} style="border: none" {{!}} '''Artikeltitel:'''
{{!}} style="width:200px; border: none;" {{!}} {{{artikeltitel}}} }}{{
#if: {{{veröffentlichungsdatum|}}}|<nowiki />
{{!}}-
{{!}} style="border: none" {{!}} '''Veröffentlichung:'''
{{!}} style="width:200px; border: none;" {{!}} {{{veröffentlichungsdatum}}}
}}
|}