Jump to content

Template:Ifexpr/doc

From mediawiki.org

With this template #ifexpr can be used in a way such that it does not strip spaces and newlines from the "then" and "else" part.

Usage

[edit]
  • {{ifexpr|2<3| p | q }} gives " p "
  • {{ifexpr|2>3| p | q }} gives " q "
  • {{ifexpr|2{{=}}3| p | q }} gives " q "
  • {{ifexpr||1=2=3| p | q }} gives " q "

Compare:

  • {{#ifexpr:2<3| 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 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):

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

Expansion depth limit

[edit]

13 levels allowed:

{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|a}}}}}}}}}}}}}}}}}}}}}}}}}} gives a

{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|{{ifexpr|1|a}}}}}}}}}}}}}}}}}}}}}}}}}}}} gives a

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 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 {{{1}}}. Templates p1 and p2 ignore {{{1}}}; template if/errmsg is only called in the case of a wrong expression {{{1}}}; it ignores p and q and uses {{{1}}} for display in the error message.

Condition

[edit]

#ifexpr

[edit]

Condition: non-zero.[1]

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

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',

See also

[edit]