Wikipedia:Conditional tables

ParserFunctions allow for the conditional display of table rows, columns or cells (and really, just about anything else). The method is fairly straightforward, but has some limitations which are worth explaining.

Basic use
The following example shows a basic use for :

Here,  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 ) with different values for :

Positional parameters   etc. work like named parameters:

Note how the pipe symbol (vertical bar) in the link works as is, it's not quite that easy within Wiki tables, see below. For more examples wrt "=" see ifdef, it doesn't affect #if:, it's a general pitfall for templates with positional parameters.

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  and the MediaWiki table syntax do not work together well. For example, the following is invalid and will not work:

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

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

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


 * - this tag creates a new row (similar to  in MediaWiki table syntax)
 * - this tag creates a new header cell within a row (similar to  in MediaWiki table syntax)
 * - this tag creates a new cell within a row (similar to  in MediaWiki table syntax)
 * - 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:

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

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 &amp;#123;, "|" by &amp;#124;, and "}" by &amp;#125;. But for Wiki tables a real "|" delimiter is required &mdash; using &amp;#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 !. 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:

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

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 one of the editors listed below.

Known bug
Using undefined positional parameters, especially or  in any colon function including parser functions like #if: fails under certain conditions.

This can be tricky for the parameter default mechanism, because then it affects only substituted colon functions.