Şablon:Xifeq
The Template:Xifeq, provides an if-equal function like parser function #ifeq, except that it uses nowiki tags to avoid the trimming of spaces and to avoid the automatic newline feature/bug in the "then" and "else" parts.
- Usage: {{xifeq|a|b|<then>|<else>}}
- {{xifeq|{{{1|27}}}|27 |{1} is 27|not 27}} → {1} is 27
- Usage: {{xifeq|a|b|<then>|<else>}}
Parameters:
- {1} - the 1st value to compare
- {2} - the 2nd value to compare to 1st
- {3} - result when true (as then-part of if)
- {4} - result when false (as else-part of if)
For small results, the processing speed will be similar to #ifeq (see below: Performance and resources).
Examples
bıvurneSome examples help to show the extended capability to handle cases such as a leading colon or leading-pound sign which triggers indented numbering when used in #ifeq:
- {{xifeq|{{{colorset|yes}}}|yes|{{{color|#f8eaba}}}}} → "#f8eaba"
- {{#ifeq:{{{colorset|yes}}}|yes|{{{color|#f8eaba}}}}} →
- f8eaba
- {{xifeq|{{{markcode|colon}}}|colon|{{{mark|:val}}}}} → ":val"
- {{#ifeq:{{{markcode|colon}}}|colon|{{{mark|:val}}}}} →
- val
Because a leading pound# (hash mark) can trigger auto-indentation as a numbered list, {{xifeq}} treats a leading "#" (or colon ":" or asterisk "*
" or semicolon ";") as being typical data, rather than indentation markup codes. As other problems arise with #ifeq, then this template can be modified to overcome those glitches and continue to work in a predictable manner.
The coding inside Template:Xifeq has been optimized to use only 2 levels of expansion nesting (see Meta "m:Help:Expansion depth"), and runs very fast.
Limitations
bıvurneThe Template:Xifeq has the following limitations: it requires special coding for equal-sign "="; it performs numeric comparison unless not numbers.
As typical with #ifeq, the 2 values are compared numerically (where "2" equals "2.0") unless the values are not numbers:
- {{xifeq| +2.00000 |2 |same as 2|not 2}} → same as 2
The numeric comparison can be avoided by putting a letter such as "z" in front of both values to compare: {{xifeq|z{{{1}}}|z{{{2}}}|...}} to force an alphanumeric comparison.
Because Template:Xifeq processes the then-clause and else-clause as template parameters, they cannot contain a simple equal-sign as in "x=x" so use either an unnamed parameter "x{{{|=}}}x" or "x{{=}}x" or set parameters by both number 3 and 4: "3=x=x" (then-clause) plus "4=not x" (else-clause).
- {{xifeq|x|x|x{{{|=}}}x|not x}} → x=x
- {{xifeq|x|x|3=x=x|4=not x}} → x=x
- {{#ifeq:x|x|x=x|not x}} → x=x
Note how the parser function #ifeq allows the simple equal-sign "x=x" whereas xifeq needs both "3=x=x" and "4=not x" to allow an equal-sign in parameter 3. For those reasons, using an unnamed parameter "x{{{|=}}}x" is often the easiest method.
The method for allowing spaces in if-logic uses 2 nowiki-slash tags "<nowiki/>" around the then-clause and else-clause: {{#ifeq:x|x|<nowiki/> yes <nowiki/>}} → " yes ". This affects using the result in string comparisons and string functions.
Performance and resources
bıvurneThe Template:Xifeq is designed to run very quickly, with minimal expansion nesting (as 2 levels) and format concise results similar to #ifeq. However, unlike #ifeq, both the then-clause and else-clause will be expanded before the comparison is made. Also, unlike #ifeq, 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 conditional text). If the then-cause and else-clause are 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:Xifeq 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.
See also
bıvurne- Template:Xifnoteq - for extended if-not-equal processing
- Template:Xifexpr - for extended if-expression processing