Help:Conditional tables

From Wikipedia, the free encyclopedia

Template:Table help

Page Module:Message box/ombox.css has no content.

There are three ways to conditionally display or hide rows within a table. All of the methods involve the use of parser conditionals, in particular, the parser Page Template:Kbd/styles.css has no content.#if statement (see the § Refresher section) to conditionally include or exclude a row, depending on the value of some expression.

Methods

CSS display:none property

The simplest option is to use the conditional with the CSS "Page Template:Kbd/styles.css has no content.display:none" property:

{| class="wikitable"
|- {{#if: {{{variable_foo|}}} | | style="display: none;" }}
! Foo
| {{{variable_foo}}}
|-
! Bar
| {{{variable_bar}}}
|}

The code above is in Template:Tl. The table below demonstrates the effect when it is used:

Template call Result
Template:Tlx
Foo {{{variable_foo}}}
Bar bar
Template:Tlx
Foo
Bar bar
Template:Tlx
Foo foo
Bar {{{variable_bar}}}
Template:Tlx
Foo foo
Bar bar

Pros and cons: with this method, there is no need for the wikitable pipe character to appear in the #if conditional, thus avoiding issues with escaping the vertical bar . However, sources and notes referred to in the hidden cells won't get suppressed with the other contents, so they will continue to be listed at the end of an article without any references to them occurring in the article's text.

HTML tables

One method of hiding rows in tables (or other structures within tables) uses HTML directly.[1] 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)

A sample HTML table with a conditional row based on testing a template variable with Page Template:Kbd/styles.css has no content.#if follows:

{| class="wikitable"
{{#if:{{{variable_foo|}}}
|<tr><th>Foo</th><td>{{{variable_foo}}}</td></tr>}}
|-
! Bar
| {{{variable_bar}}}
|}

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

Template call Result
Template:Tl
Bar {{{variable_bar}}}
Template:Tlp
Bar {{{variable_bar}}}
Template:Tlp
Bar bar
Template:Tlp
Foovalue
Bar {{{variable_bar}}}
Template:Tlp
Foovalue
Bar bar

Vertical bar escaping

Because the parser function Page Template:Kbd/styles.css has no content.#if and MediaWiki table syntax do not work together well, the template code described at § Pitfalls with an illustration of non-working code at Example 2 will not work correctly. The problems there can be rectified by properly escaping the vertical bar.

For most characters, often it's good enough to replace a problematic characters with the corresponding HTML entities, e.g. "{" by &#123;, "|" by &#124;, and "}" by &#125;. But for Wiki tables a real vertical bar character "|" is required — using &#124; HTML entity does not work.

A simple trick allows to protect the "|" in template parameter values while still arriving as real "|" delimiter in the Wiki table, see the magic word {{!}}. 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="wikitable"
{{#if:{{{foo|}}}|
{{!}}-
! Foo
{{!}} {{{foo}}}
}}
|-
! Bar
| {{{bar}}}
|}

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

Template call Result
Template:Tlx
Bar nobar
Template:Tlx
Bar vbar
Template:Tlx
Foo value
Bar {{{bar}}}
Template:Tlx
Foo value
Bar vbar

Refresher on parser functions

Script error: No such module "labelled list hatnote".

Conditional parser functions like Page Template:Kbd/styles.css has no content.#if allow for the conditional display of table rows, columns or cells, but they have some limits.

The following example shows a basic use for #if that is available from the extension ParserFunctions:

 {{#if: {{{variable_foo|}}}
   |foo is set to '''{{{variable_foo}}}'''
   |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 Template:Tlx) with different values for {{{variable_foo}}}:

Template call Result
Template:Tlx foo is blank
Template:Tlx foo is blank
Template:Tlx foo is set to value

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

 {{#if:{{{1|}}}
 |1st parameter is '''{{{1}}}'''
 |1st parameter is ''blank''}}
Template call Result
Template:Tlx 1st parameter is blank
Template:Tlx 1st parameter is foo
Template:Tlx 1st parameter is not empty
Template:Tlx 1st parameter is blank
Template:Tlx 1st parameter is ok=yes

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

Pitfalls

Parser functions and table syntax

Unfortunately #if and the MediaWiki table syntax do not work together well. For example, the following, Template:Tl is invalid and will not work:

{| class="wikitable"
{{#if:{{{variable_foo|}}}
|
|-
! Foo
| {{{variable_foo}}}
}}
|-
! Bar
| {{{variable_bar}}}
|}

The table below demonstrates the effect when Template:Tl is used:

Template call Result
Template:Tl
Foo
Bar {{{variable_bar}}}
Template:Tlp
Foo
Bar {{{variable_bar}}}
Template:Tlp
Foo
Bar bar
Template:Tlp
Bar {{{variable_bar}}}
Template:Tlp
Bar bar

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

Getting help

If you find yourself unable to get a template to behave how you like, you can try asking on Village pump, placing a request at Requested templates, or contacting an editor via IRC.

See also

For avoiding blank rows in the case of successive optional rows, see m:Help:Table#Conditional table row.

For more information on #if (and other # functions), see:

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:

Notes and references

Page Template:Reflist/styles.css has no content.

  1. ^ Using HTML table code in templates can make them non-portable to other MediaWiki wikis. This is because Wikipedia and other Wikimedia Foundation wikis process wikitext through HTML Tidy; most other wikis do not have the same setup, and the HTML table tags do not render. See Wikipedia:WikiProject Transwiki#Special templates.

Lua error in package.lua at line 80: module 'Module:Navbox/configuration' not found.