Template:Xifexpr/doc

The Template:Xifexpr provides a quick, extended if-expression function, to compensate for any performance limitations, beyond the parser function #ifexpr.
 * Usage:
 * &rarr;

Parameters:
 * {1} - the expression to evaluate as an if-test
 * {2} - result when expression is true (as then-part of if)
 * {3} - result when expression is false (as else-part of if)

For small results, the processing speed will be similar to #ifexpr (see below: Performance and resources).

Examples
Some examples help to show the extended capability to handle cases such as a leading colon or leading-pound sign which might trigger indented numbering when used in #ifexpr:
 * &rarr;


 * "" &rarr; ""
 * "" &rarr; ""


 * &rarr;
 * &rarr;


 * &rarr;
 * &rarr;


 * &rarr;


 * text &rarr; text
 * text &rarr; text

Because a leading pound# (hash mark) can trigger auto-indentation as a numbered list, treats a leading "#" (or colon ":" or asterisk " " or semicolon ";") as being typical data, rather than indentation markup codes. As other problems arise with #ifexpr, then this template can be modified to overcome those glitches and continue to work in a predictable manner.

The coding inside Template:Xifexpr has been optimized to use only 2 levels of expansion nesting (see "m:Help:Expansion depth"), and runs very fast.

Limitations
The Template:Xifexpr has the following limitations: it requires special coding for equal-sign "="; it performs numeric comparison of parameters unless they are compared using a {#ifeq} test, as nested inside the ifexpr, {ifexpr|{#ifeq:aa|bb|1}|...|...}.

As typical with #ifexpr, the values are compared numerically (where "2" equals "2.00") as in the following comparison:
 * &rarr;

The numeric comparison can be avoided by putting a letter such as "z" in front of both values and comparing them, first, within a nested {#ifeq}: {&#123;xifexpr| {&#123;#ifeq: z{&#123;{1}}}|z{&#123;{2}}}|1 }}|&lt;then>|&lt;else>}} to force an alphanumeric comparison of each 2 parameters (resulting as "1" meaning true when equal), within the overall expression.

Because Template:Xifexpr processes the then-clause and else-clause as template parameters, they cannot contain a simple equal-sign as in "x=1" so use either an unnamed parameter containing "=" as in "x{&#123;{|=}&#125;}1" or use Template:= as in "x{&#123;=&#125;}x" or set parameters by both parameter number 2 and 3: "2=x=1" (then-clause) plus "3=not 1" (else-clause).


 * &rarr;
 * &rarr;
 * &rarr;

Note how the parser function #ifexpr allows the then-clause or else-clause to contain a simple equal-sign (as in "x=1"), whereas Xifexpr needs both "2=x=1" and "3=not 1" to allow an equals-sign in parameter 2. For those reasons, using an unnamed parameter "x{&#123;{|=}&#125;}1" is often the easiest method.

Performance and resources
The Template:Xifexpr is designed to run very quickly, with minimal expansion nesting (as 2 levels) and format concise results similar to #ifexpr. However, unlike #ifexpr, both the then-clause and else-clause will be expanded before the expression is evaluated. Also, unlike #ifexpr, the intermediate expansion sizes of both the then-clause and else-clause are both counted, and counted double, against the resource post-expand size (of 2,000 kb of conditional text). If the then-cause and else-clause were roughly equal in size, the post-expand size would be 4x times that size. However, only the actual choice of then-clause or else-clause is formatted into the final page; the post-expand size is just an intermediate limit for the MediaWiki preprocessor.

During intermediate processing, Template:Xifexpr requires the formatting of both the then-part and else-part, before choosing between the 2 sets of formatted results. In most cases, there is no significant speed delay because hundreds of templates can be processed very quickly before choosing between 2 if-else branches.

The method for allowing spaces in if-logic, in January 2011, used 2 nowiki-slash tags "&lt;nowiki/>" around the then-clause and else-clause: &rarr; " yes ".