(Translated by https://www.hiragana.jp/)
Template:If - Meta Jump to content

Template:If

From Meta, a Wikimedia project coordination wiki
Template documentation

With this template the parser functions of Extension:ParserFunctions with names starting with "#if" 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

[edit]
Using this {{if|*|...}} template, with {{=}} where necessary Compare to using a {{#if*:...}} parser function
2 parameters:
  • "{{if|error| GOOD }}" gives " GOOD "
  • "{{if|error| <span class{{=}}"good">x</span> }}" gives " x "
  • "{{if|error| <span class{{=}}"error">x</span> }}" gives ""
1 parameter:
  • "{{#iferror: GOOD }}" gives "GOOD"
  • "{{#iferror: <span class="good">x</span> }}" gives "x"
  • "{{#iferror: <span class="error">x</span> }}" gives ""
3 parameters:
  • "{{if|error| 7 | ERROR }}" gives " 7 "
  • "{{if|error| <span class{{=}}"error">x</span> | ERROR }}" gives " ERROR "
2 parameters:
  • "{{#iferror: 7 | ERROR }}" gives "7"
  • "{{#iferror: <span class="error">x</span> | ERROR }}" gives "ERROR"
4 parameters:
  • "{{if|error| 7 | ERROR | ALT }}" gives " ALT "
  • "{{if|error|<span class{{=}}"error">x</span>| ERROR | ALT }}" gives " ERROR "
  • "{{if|error| 7 | ERROR | }}" gives " "
  • "{{if|error| 7 | ERROR |}}" gives ""
  • "{{if|error| 7 | ERROR }}" gives " 7 "
  • "{{if||x| YES | NO }}" gives " YES "
  • "{{if|| | YES | NO }}" gives " NO "
  • "{{if||| YES | NO }}" gives " NO "
  • "{{if|expr|2<3| YES | NO }}" gives " YES "
  • "{{if|expr|2>3| YES | NO }}" gives " NO "
  • "{{if|expr|2{{=}}3| YES | NO }}" gives " NO "
  • "{{if|exist| Help:Link | YES | NO }}" gives " YES "
  • "{{if|exist| Hulp:Link | YES | NO }}" gives " NO "
  • "{{if|eq| u |u| YES }}" gives " YES "
  • "{{if|eq| u |v| YES }}" gives ""
3 parameters:
  • "{{#iferror: 7 | ERROR | ALT }}" gives "ALT"
  • "{{#iferror:<span class="error">x</span>| ERROR | ALT }}" gives "ERROR"
  • "{{#iferror: 7 | ERROR | }}" gives ""
  • "{{#iferror: 7 | ERROR |}}" gives ""
  • "{{#iferror: 7 | ERROR }}" gives "7"
  • "{{#if:x| YES | NO }}" gives "YES"
  • "{{#if: | YES | NO }}" gives "NO"
  • "{{#if:| YES | NO }}" gives "NO"
  • "{{#ifexpr:2<3| YES | NO }}" gives "YES"
  • "{{#ifexpr:2>3| YES | NO }}" gives "NO"
  • "{{#ifexpr:2=3| YES | NO }}" gives "NO"
  • "{{#ifexist: Help:Link | YES | NO }}" gives "YES"
  • "{{#ifexist: Hulp:Link | YES | NO }}" gives "NO"
  • "{{#ifeq: u |u| YES }}" gives "YES"
  • "{{#ifeq: u |v| YES }}" gives ""
5 parameters:
  • "{{if|eq| u |u| YES | NO }}" gives " YES "
  • "{{if|eq| u |v| YES | NO }}" gives " NO "
4 parameters:
  • "{{#ifeq: u |u| YES | NO }}" gives "YES"
  • "{{#ifeq: u |v| YES | NO }}" gives "NO"

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

Alternatively you may want pass the condition containing the equal sign by explicitly giving the parameter 2 by name, but then the unnumbered parameters after it after it would override the condition (and you don't want to pass them by name because their value would have their whitespace trimmed), so you still need to pass the unnumbered parameter 2, and override it with the explicitly named parameter, but this adds a warning during expansion because the parameter 2 is then passed twice (only the last value is taken into account).

For full substitution, simply use subst: or safesubst:. 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 {{safesubst:Expression error: Unexpected number.|expr|1234>e3|1|0|}}
  • {{#ifexpr:1234>e3|1|0}} gives Expression error: Unexpected number.

Internal technical details

[edit]

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 only 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.

Expansion depth limit

[edit]

Without the expansion depth limit, all these should return the same result "a".

1 level:

  • "{{if||x|a}}" gives "a".
  • "{{if||||a}}" gives "a".

2 levels:

  • "{{if||x|{{if||x|a}}}}" gives "a".
  • "{{if||||{{if||||a}}}}" gives "a".

3 levels:

  • "{{if||x|{{if||x|{{if||x|a}}}}}}" gives "a".
  • "{{if||||{{if||||{{if||||a}}}}}}" gives "a".

6 levels:

  • "{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|a}}}}}}}}}}}}" gives "a".
  • "{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||a}}}}}}}}}}}}" gives "a".

9 levels:

  • "{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|a}}}}}}}}}}}}}}}}}}" gives "a".
  • "{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||a}}}}}}}}}}}}}}}}}}" gives "a".

10 levels:

  • "{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|a}}}}}}}}}}}}}}}}}}}}" gives "a".
  • "{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||a}}}}}}}}}}}}}}}}}}}}" gives "a".

11 levels:

  • "{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|a}}}}}}}}}}}}}}}}}}}}}}" gives "a".
  • "{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||a}}}}}}}}}}}}}}}}}}}}}}" gives "a".

Conditions

[edit]

#if:

[edit]

Condition: string (after trimming leading and trailing spaces) not empty.

#ifexist:

[edit]

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:

[edit]

Condition: non-zero.[1]

Empty gives false. Other strings which are not correct numerical or Boolean expressions give an error message[2]:

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

#iferror:

[edit]

Condition: contains class="error" as attribute of <strong>, <span>, <p>, or <div> tags.[3]

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:

[edit]

Condition: equal as strings (case-sensitive) or numbers.[4]

  • {{if|eq| 1e3 | 1000 | 1 | 0 }} gives " 1 ".
  • {{#ifeq: 1e3 | 1000 | 1 | 0 }} gives "1".

To force a string-based comparison, add a non-numerical character to both compared arguments.

  • {{if|eq| x1e3 | x1000 | 1 | 0 }} gives " 0 ".
  • {{#ifeq: x1e3 | x1000 | 1 | 0 }} gives "0".

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

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

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

See also

[edit]
  1. Expr.php
  2. ParserFunctions.i18n.php
  3. ParserFunctions_body.php
  4. #ifeq: uses the PHP comparison operator ==.[1]