Template:Ifequal

From imedwiki
Jump to navigation Jump to search
Template documentation[view] [edit] [history] [purge]

This template is used to extend the parser function #ifeq: to multiple tests.

The template takes up to eight pairs of arguments (i.e., 16 unnamed parameters), tests the odd versus the even parameter values, and returns a TRUE value (a string of characters) if any pair matches using the parser function #ifeq:, otherwise it returns a FALSE value (an empty string).

  • {{ifequal|foo|foo}} → 1-2
  • {{ifequal|foo|bar}}
  • {{ifequal|foo|foo|bar|bar}} → 3-41-2
  • {{ifequal|foo|foo|bar|buzz}} → 1-2
  • {{ifequal|foo|bar|buzz|buzz}} → 3-4
  • {{ifequal|foo|bar|buzz|bat}}

The returned values are suitable for testing within simple if-then-else conditionals inside the calling template.

Typical example

{{#if: {{ifequal|{{{1}}}|one|{{{2}}}|two|{{{3}}}|three}} | Yes, we | No, we don't }} have a match.

Assuming this code is inside of a template that can be called with three unnamed parameter values:

  • The {{ifequal}} call returns TRUE if the first unnamed parameter has the value "one" OR the second has the value "two" OR the third has the value "three", in which case the code will build the string: "Yes, we have a match."
  • The {{ifequal}} call returns FALSE if none match, in which case it will build the string: "No, we don't have a match."

OR detection

  1. An equality: {{#if:{{ifequal|1|2|3|4|6|6}}|True|False}} -- [True]
  2. An equality, where {{{1}}} is an example variable: {{#if:{{ifequal|{{{1}}}|1|{{{1}}}|2|{{{1}}}3|{{{1}}}|4|{{{1}}}|5}}|True|False}} -- [True] when the variable value is 1, 2, 3, 4 or 5. [False] when is any another value.
  3. Inequality: {{{{#if:ifequal|1|2|3|4|5|6}}|True|False}} -- [False]

Behavior with empty strings (unset parameter values)

Assuming the parameter values referred to here have not been set in the calling template:

  • {{ifequal||}}
  • {{ifequal|{{{foo|}}}|}}
  • {{ifequal|{{{foo|}}}|{{{bar|}}}}}

Note how empty strings are not considered equal in any of these cases.

But beware: in the following two examples, if the parameters have not been set, the literal strings "{{{foo}}}" and "{{{bar}}}" are used in their place.

  • {{ifequal|{{{foo}}}|}}
  • {{ifequal|{{{foo}}}|{{{bar}}}}}

The difference is the presence or absence of a vertical-bar character (|) following "foo" and "bar". See Help:Template#Handling parameters for more information about this.

To detect whether parameter values are set or unset, use the Page Template:Mono/styles.css has no content.#if: parser function instead of this template.

See also

  • {{ifeqall}} — Compares one comparison string against up to 10 case strings with optional result string and default result string outputs.
  • {{ifeqany}} — Compares up to 24 case strings against each other with result string and default result string outputs.
  • {{ifor}} — Compares up to 30 comparison strings against an equal number of case strings with optional result string and default result string outputs.
  • {{}} — Returns an equal sign.
  • Wikipedia:Advanced template coding
This template is based (in parts) on the Template:Ifequal from the free encyclopedia wikipedia and is licensed under GNU license for free documentation and the Creative Commons Attribution/Share Alike. On wikipedia there is a List of authors accessible. More about importing from wikipedia on page Imedwiki:Importing from wikipedia.