Template:If
{{ safesubst:p{{ safesubst:#if{{{1}}}:{{{2}}}|1|2}}|{{{3}}}|}}
MediaWiki's stock parser functions with names starting with "#if" trim spaces and newlines from the "then" and "else" parts. This mediawiki.org template is a wrapper easing their use in a way that prevents such trimming.
Spaces still do not affect the outcome of the condition. Parameter 1 selects the if-type as "eq", "expr", "exist" or "error" (for #iferror), or empty "||" for a simple if-there (for #if). The template can be repeatedly nested 6 or 7 levels, one inside the other, because the outer-most is completed before running either the then/else inner levels.
This template can be substituted when the expression or comparison will not change. Functionally, a lead-space character is stored, internally, as a simple blank character. Note that in many cases a null nowiki tag ("<nowiki/>") could be used, without Template:If, to allow a lead-space (such as "<nowiki/> xx") anywhere, but the internal storage puts a 43-character marker for "<nowiki/>" in string length. However, there is no extra expansion depth for a nowiki-tag, such as in trailing space, "zz <nowiki/>".
Usage
editWhere p is implemented in affirmation of the statement and q is implemented in negation of the statement
- {{if||x| p | q }} gives " p ". Notice the pair of vertical bars surrounding the empty first parameter.
- {{if|expr|2<3| p | q }} gives " p "
- {{if|eq| u |u| p | q }} gives " p "
- {{if|exist| Help:Link | p | q }} gives " p "
- {{if|error|{{#expr:x}}| p | q }} gives " p "
Compare:
- {{#if:x| p | q }} gives "p"
- {{#ifexpr:2<3| p | q }} gives "p"
- {{#ifeq: u |u| p | q }} gives "p"
- {{#ifexist: Help:Link | p | q }} gives "p"
- {{#iferror:{{#expr:x}}| p | q }} gives "p"
For example, with a template named Template:Template name containing the code {{#ifeq: {{{parameter}}} |u| p | q }}, entering the code {{{Template name|parameter=u}}} into an article gives "p", but {{{Template name|parameter=v}}} gives "q".
The text of either the then-clause or else-clause is only processed and expanded when triggered. Hence, any templates in use are only expanded once the then-clause or else-clause is matched, otherwise they are skipped as merely paired braces, "{{" with "}}".
Indentation: If indenting the markup, care must be taken to avoid extra newlines when indenting "}}" on the next line. An extra bar pipe "|" can be added after the else-clause to complete that text, and allow "}}" to then be placed anywhere without adding a newline into the else-clause. For example:
- {{if|{{{1|}}}
|then found parameter 1|else no parameter 1| ← extra "|" ends else-clause
}}
- {{if|{{{1|}}}
When the else-clause is indented to the next line, a newline (CR/LF) is added:
- {{if|{{{1|}}}
|then found parameter 1 ← extra newline here
|else noparameter 1|
}}
- {{if|{{{1|}}}
To indent the else-clause, split an HTML-form comment, as "<!--" with next line as "-->|else...". Unless each then-clause and else-clause is carefully tested, to watch for extra newlines, then the results are likely to cause broken lines, with extra line breaks for each newline. For that reason, a global edit with simple search-and-replace of "{#if:
" to "{if||
" is likely to leave newline problems, wherever the original markup was wrapped to indent either the else-clause or "}}" of each if-structure. Indenting the then-clause is not a problem.