Template:Coalesce

From Pittsburgh Streets
The {{coalesce}} template takes up to 10 arguments and returns the first nonempty one.

This can be useful to support synonyms in a template. For example, suppose a template named {{foo}} takes a parameter named color, with colour as a synonym. It is tempting to write {{{color|{{{colour|}}}}}}, but this doesn't work right if both color is specified but empty, like this:

{{foo
 |color=
 |colour=grey
}}

In such a case, {{{color|...}}} will produce the empty string, without evaluating ..., because the criterion for using the default value ... is that color is not specified, not that color is empty (see [1]). The right way to handle this is to use {{coalesce}}:

{{coalesce|{{{color|}}}|{{{colour|}}}}}

Usage examples

  • {{coalesce}}
  • {{coalesce|a}} → a
  • {{coalesce|a|b|c}} → a
  • {{coalesce||b|c}} → b
  • {{coalesce||||||||||j}} → j
  • {{coalesce||||||||||}}
  • {{coalesce|1=a|2=b}} → a
  • {{coalesce|1=|2=b}} → b
  • {{coalesce|1=|2=|3=|4=|5=|6=|7=|8=|9=|10=j}} → j
  • {{coalesce|1=|2=|3=|4=|5=|6=|7=|8=|9=|10=}}