Template:If

Documentation

Template:Documentation subpage With this template the parser functions of Extension:ParserFunctions with names starting with #if (those, apart from #switch, with the functionality of a conditional expression) can be used in a way such that they do not strip spaces and newlines from the "then" and "else" part. For evaluating the condition spaces and newlines are stripped like with ParserFunctions.

Usage

  • 4 parameters:
    • {{if||x| p | q }} gives " p "
    • {{if||| p | q }} gives " q "
    • {{if|expr|2<3| p | q }} gives " p "
    • {{if|expr|2>3| p | q }} gives " q "
    • {{if|expr|2{{=}}3| p | q }} gives "Template:If/errmsg"
    • {{if|expr||2=2=3| p | q }} gives " q "
    • {{if|exist| Help:Link | p | q }} gives " q "
    • {{if|exist| Hulp:Link | p | q }} gives " q "
    • {{if|error|<span class{{=}}"error">x</span>| p | q }} gives " q "
    • {{if|error| 7 | p | q }} gives " q "
    • {{if|error| 7 | p | }} gives " "
    • {{if|error| 7 | p |}} gives ""
    • {{if|error| 7 | p }} gives "Template:P3"
  • 5 parameters:
    • {{if|eq| u |u| p | q }} gives " p "
    • {{if|eq| u |v| p | q }} gives " q "

Compare:

  • 3 parameters:
    • {{#if:x| p | q }} gives "p"
    • {{#ifexpr:2<3| p | q }} gives "p"
    • {{#ifexist: Help:Link | p | q }} gives "q"
    • {{#iferror:<span class="error">x</span>| p | q }} gives "p"
    • {{#iferror: 7 | p | q }} gives "q"
    • {{#iferror: 7 | p | }} gives ""
    • {{#iferror: 7 | p |}} gives ""
    • {{#iferror: 7 | p }} gives "7"
  • 4 parameters:
    • {{#ifeq: u |u| p | q }} gives "p"

Note that, unlike parser functions, templates require writing equals signs in values of unnamed parameters as {{=}}

For full substitution, simply use Template:Cnw or Template:Cnw. If the condition contains a parser function, template, or variable, that should be substituted too. Optionally the "then" or "else" part can also be substituted.

Error messages (examples):

  • {{if|expr|1234>e3|1|0}} gives Template:If/errmsg
  • {{#ifexpr:1234>e3|1|0}} gives Expression error: Unexpected number.

Expansion depth limit

7 levels allowed:

{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|a}}}}}}}}}}}}}} gives a

{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|a}}}}}}}}}}}}}}}} gives Template:If/errmsg

{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||a}}}}}}}}}}}}}} gives a

{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||a}}}}}}}}}}}}}}}} gives Template:If/errmsg

{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|a}}}}}}}}}}}}}} gives a

{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|a}}}}}}}}}}}}}}}} gives Template:If/errmsg

0{{if||x|1{{if||x|2{{if||x|3{{if||x|4{{if||x|5{{if||x|6{{if||x|7}}6}}5}}4}}3}}2}}1}}0 gives 012345676543210

0{{if||x|1{{if||x|2{{if||x|3{{if||x|4{{if||x|5{{if||x|6{{if||x|7{{if||x|8}}7}}6}}5}}4}}3}}2}}1}}0 gives 01234567Template:If/errmsg76543210

When the limit is reached templates are not expanded anymore. The braces are preserved, and an error message is inserted. In this case this affects the index value in the switch in the template (see below). As a result the default value if/errmsg is produced, which is also intended for the case that the first parameter is "expr" and the second parameter is not a valid expression: in that case the result of applying #ifexpr is neither the then-part p1 nor the else-part p2. This gives an error message produced by Template:If/errmsg, which in the case that the template limit is not exceeded shows the wrong expression and the error message of the system. If the template limit is exceeded the template attempts to show the condition which is not evaluated, and whether it is actually true or false. This may either be the actual condition, or it may itself be affected by the template limit.

To allow more levels there are the separate Template:Ifnotempty, Template:Ifexpr, and Template:Ifeq.

Internal technical details

For evaluating the condition the template uses the actual parser functions; however, not directly with p and q, because this strips spaces and newlines from them, but with index values 1 and 2. The result can be 1, 2, or, only in the case of #ifexpr, an error message. Using a switch these are converted to p1, p2, and if/errmsg, respectively. The template with the applicable name is called (Template:P1, Template:P2, or Template:If/errmsg), with as parameters p, q, and {{{2}}}. Templates p1 and p2 ignore {{{2}}}; template if/errmsg is called in the case of #ifexpr, in the case of a wrong expression {{{2}}}; it ignores p and q and uses {{{2}}} for display in the error message.

Conditions

#ifexpr has directly the functionality of a conditional expression. #if, #ifexist, #iferror and #ifeq each combine this with a Boolean function. For the Boolean function itself use 1 as then-part and 0 as else-part.

#if

Condition: string not empty.

#ifexist

Condition: page exists (applies also to special pages).

The usual case-sensitivity applies: if a page exists then also a non-canonical name for that page gives a positive result.

Template:Exists gives the same result, except that the result is positive for an interwiki link. You can also handle an interwiki link differently with Template:If interwiki link.

The page A checking existence of page B is listed on Special:Whatlinkshere/B as a page which links to B.

On Wikimedia wikis there is a maximum for the number of distinct pages for which #ifexist is called on a page. A call of #ifexist after a transclusion of the same page does not require an extra database query and is therefore not counted. A call beyond the maximum returns "false". The html-source of each page shows the counted number of calls, as well as the maximum. The maximum is currently 500, see w:Wikipedia:Template limits#Expensive parser function calls.

#ifexpr

Condition: non-zero.<ref>Expr.php</ref>

Empty gives false. Other strings which are not correct numerical or Boolean expressions give an error message<ref>ParserFunctions.i18n.php</ref>:

16 	'pfunc_expr_stack_exhausted' => 'Expression error: Stack exhausted',
17 	'pfunc_expr_unexpected_number' => 'Expression error: Unexpected number',
18 	'pfunc_expr_preg_match_failure' => 'Expression error: Unexpected preg_match failure',
19 	'pfunc_expr_unrecognised_word' => 'Expression error: Unrecognised word "$1"',
20 	'pfunc_expr_unexpected_operator' => 'Expression error: Unexpected $1 operator',
21 	'pfunc_expr_missing_operand' => 'Expression error: Missing operand for $1',
22 	'pfunc_expr_unexpected_closing_bracket' => 'Expression error: Unexpected closing bracket',
23 	'pfunc_expr_unrecognised_punctuation' => 'Expression error: Unrecognised punctuation character "$1"',
24 	'pfunc_expr_unclosed_bracket' => 'Expression error: Unclosed bracket',
25 	'pfunc_expr_division_by_zero' => 'Division by zero',
26 	'pfunc_expr_invalid_argument' => 'Invalid argument for $1: < -1 or > 1',
27 	'pfunc_expr_invalid_argument_ln' => 'Invalid argument for ln: <= 0',
28 	'pfunc_expr_unknown_error' => 'Expression error: Unknown error ($1)',
29 	'pfunc_expr_not_a_number' => 'In $1: result is not a number',

#iferror

Condition: contains class="error" as attribute of <strong>, <span>, <p>, or <div> tags.<ref>ParserFunctions_body.php</ref>

These are generated by #expr, #ifexpr, #time, #rel2abs, some other MediaWiki situations (such as recursive depth and template loop checks), and this template.

If the error condition does not apply and there is no "else" part, the function gives the input wikitext.

#ifeq

The following applies also to Template:If and Template:Ifeq.

Condition<ref>#ifeq: uses the PHP comparison operator ==.[1]</ref>:

  • if both strings are numbers: equal numbers
  • otherwise: equal strings (case-sensitive)

Examples where the condition is true while the strings are not equal:

However:

This is because 1e-3 is considered a number, while 1/1e3 is an expression, evaluated with #expr and #ifexpr, but not with #ifeq.

Note also that e and pi are not numbers but operators without argument, therefore string comparison is applied:

For strict comparison of strings, add a non-numerical character to both compared arguments.

If desired, trim the arguments before adding this non-numerical character, for example with {{#if:x|..}}.

Text between <nowiki> tags is temporarily replaced by a unique code. This affects comparisons:

  • {{#ifeq:<nowiki>abc</nowiki> | <nowiki>abc</nowiki> | 1 | 0 }} gives "0".

See also

Template:Refs

<div style="clear:both;" />
</div>