Jump to content

Help:Conditional tables

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Locke Cole (talk | contribs) at 12:12, 13 April 2006 (add WP:IF shortcut (will eventually move this page and rewrite to describe uses of #if)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Please note: This describes a method of hiding rows or data inside templates which will, ultimately, be replaced by native syntax in the MediaWiki software. As such, please bear in mind that once the software is updated, you should cease using this method for hiding data. Shortcut:
WP:QIF
WP:IF

The {{qif}} template (as well as a number of other templates which directly or indirectly rely on the methods used in qif) allows for conditional display of table rows, columns or cells. The method is fairly straightforward, but has some limitations which are worth explanation.

Basic use

The following example shows a basic use for {{qif}}:

{{qif
|test={{{variable_foo|}}}
|then=foo is set to '''{{{variable_foo}}}'''
|else=foo is ''blank''}}

Here, {{{variable_foo}}} is checked to see if it is defined with a non-blank value. The table below shows the output from a template call (we'll call the template {{qif_example_1}}) with different values for {{{variable_foo}}}:

Template call Result
{{qif_example_1}} Template:Qif example 1
{{qif_example_1|variable_foo=}} Template:Qif example 1
{{qif_example_1|variable_foo=value}} Template:Qif example 1

Positional parameters {{{1}}} etc. work like named parameters:

{{qif
|test={{{1|}}}
|then=1st parameter is '''{{{1}}}'''
|else=1st parameter is ''blank''}}
Template call Result
{{Qif_example_1/b| |bar}} Template:Qif example 1/b
{{Qif_example_1/b|foo|bar}} Template:Qif example 1/b
{{Qif_example_1/b| [[m:|not empty]] }} Template:Qif example 1/b

Note how the pipe symbol (vertical bar) in the link works as is, it's not quite that easy within Wiki tables, see below.

Hiding rows entirely

It's also possible to hide rows of data within a table, however, there are issues you should be aware of.

Incorrect usage

Unfortunately {{qif}} and the MediaWiki table syntax do not work together well. For example, the following is invalid and will not work:

{| class="infobox"
{{qif
|test={{{variable_foo|}}}
|then=
|-
! Foo
| {{{variable_foo}}}
}}
|-
! Bar
| {{{variable_bar}}}
|}

The code above is in {{qif_example_2}}. The table below demonstrates the effect when it's used:

Template call Result
{{qif_example_2}} Template:Qif example 2
{{qif_example_2|variable_foo=}} Template:Qif example 2
{{qif_example_2|variable_foo=|variable_bar=bar}} Template:Qif example 2
{{qif_example_2|variable_foo=value}} Template:Qif example 2
{{qif_example_2|variable_foo=value|variable_bar=bar}} Template:Qif example 2

The problem is with the usage of the pipe character (|). This character, in template calls, is used to separate parameters and so is invalid.

Correct usage

Plan A

One method of hiding rows in tables (or other structures within tables) utilizes HTML directly. HTML is more complicated than MediaWiki table syntax, but not much more so. In general, there are only a handful of HTML tags you need to be aware of

  • <tr> - this tag creates a new row (similar to |- in MediaWiki table syntax)
  • <th> - this tag creates a new header cell within a row (similar to ! in MediaWiki table syntax)
  • <td> - this tag creates a new cell within a row (similar to | in MediaWiki table syntax)
  • <caption> - this tag creates a caption (similar to |+ in MediaWiki table syntax)

Working from the invalid template example above, by switching to HTML we end up with the following code:

{| class="infobox"
{{qif
|test={{{variable_foo|}}}
|then=<tr><th>Foo</th><td>{{{variable_foo}}}</td></tr>}}
|-
! Bar
| {{{variable_bar}}}
|}

The code above is in {{qif_example_2a}}. As before, the table below demonstrates the effect when it's used:

Template call Result
{{qif_example_2a}} Template:Qif example 2a
{{qif_example_2a|variable_foo=}} Template:Qif example 2a
{{qif_example_2a|variable_foo=|variable_bar=bar}} Template:Qif example 2a
{{qif_example_2a|variable_foo=value}} Template:Qif example 2a
{{qif_example_2a|variable_foo=value|variable_bar=bar}} Template:Qif example 2a

Plan B

As noted above the only problem is the vertical bar or pipe symbol within a template. Often it's good enough to replace problematic characters by numeric references, e.g. "{" by &#123;, "|" by &#124;, and "}" by &#125;. But for Wiki tables a real "|" delimiter is required, using &#124; doesn't work as delimiter.

A simple trick allows to protect the "|" in template parameter values while still arriving as real "|" delimiter in the Wiki table, see Template:!(edit talk links history). Note that "!" (exclamation mark) has no problems with templates, it's the other delimiter used in Wiki tables. Here's the code for plan B:

{| class="infobox"
{{qif|test={{{foo|}}}|then=
{{!}}-
! Foo
{{!}} {{{foo}}}
}}
|-
! Bar
| {{{bar}}}
|}

The code above is in {{qif_example_2b}}. As before, the table below demonstrates the effect when it's used:

Template call Result
{{qif_example_2b|baz=nobar}} Template:Qif example 2b
{{qif_example_2b|foo=|bar=vbar}} Template:Qif example 2b
{{qif_example_2b|foo=value}} Template:Qif example 2b
{{qif_example_2b|foo=value|bar=vbar}} Template:Qif example 2b

Getting help

If you find yourself unable to get a template to behave how you like, you can try asking on Village pump, or contacting one of the editors listed below.

Editors willing to help

The following editors welcome questions about template design. Leave them a message on their talk page if you need help; be sure to give a link to your template (or, if you haven't created it yet, provide the code you tried to use that didn't work). If you would like someone to make a template for you, be sure to provide some details about what you'd like in the template.

See also

The following help topics deal with templates:

This help topic deals with table design (since most templates use tables, this may be useful):

And finally, a sample template which demonstrates usage of {{qif}} is available: