(uses safesubst now) |
(expanded to note parameter 1 and "| |" form; can be nested; added section "Indentation" to explain how to indent the markup without extra newlines.) |
||
Line 1: | Line 1: | ||
{{Documentation subpage}}<includeonly>{{pp-template|small=yes}}</includeonly> | {{Documentation subpage}}<includeonly>{{pp-template|small=yes}}</includeonly> | ||
With this template the [[Help:parser function|parser functions]] of the [[mw:Help:Extension:ParserFunctions|ParserFunctions]] collection with names starting with "#if" can be used in a way such that they do not [[m:Help:Newlines_and_spaces#Stripping_on_expansion|strip spaces and newlines]] from the "then" and "else" part. Spaces still do not affect the outcome of the condition. | With this template the [[Help:parser function|parser functions]] of the [[mw:Help:Extension:ParserFunctions|ParserFunctions]] collection with names starting with "#if" can be used in a way such that they do not [[m:Help:Newlines_and_spaces#Stripping_on_expansion|strip spaces and newlines]] from the "then" and "else" part. Spaces still do not affect the outcome of the condition. Parameter 1 selects the if-type as "eq", "expr", "exists" or "error" (for #iferror), or empty "||" for a simple if-there (for #if). The template can be repeatedly nested, one inside the other, because the outer-most is completed before running either the then/else inner levels. | ||
This template can be substituted. | This template can be substituted. | ||
Line 7: | Line 7: | ||
==Usage== | ==Usage== | ||
*<nowiki>{{if||x| p | q }}</nowiki> gives "{{if||x| p | q }}". Notice the ''pair'' of vertical bars surrounding the empty first parameter. | *<nowiki>{{if||x| p | q }}</nowiki> gives "{{if||x| p | q }}". Notice the ''pair'' of vertical bars surrounding the empty first parameter. | ||
*<nowiki>{{if|expr|2<3| p | q }}</nowiki> gives " p " | *<nowiki>{{if|expr|2<3| p | q }}</nowiki> gives " p " | ||
*<nowiki>{{if|eq| u |u| p | q }}</nowiki> gives "{{if|eq| u |u| p | q }}" | *<nowiki>{{if|eq| u |u| p | q }}</nowiki> gives "{{if|eq| u |u| p | q }}" | ||
Line 18: | Line 19: | ||
*<nowiki>{{#ifexist: Help:Link | p | q }}</nowiki> gives "{{#ifexist: Help:Link | p | q }}" | *<nowiki>{{#ifexist: Help:Link | p | q }}</nowiki> gives "{{#ifexist: Help:Link | p | q }}" | ||
*<nowiki>{{#iferror:{{#expr:x}}| p | q }}</nowiki> gives "{{#iferror:{{#expr:x}}| p | q }}" | *<nowiki>{{#iferror:{{#expr:x}}| p | q }}</nowiki> gives "{{#iferror:{{#expr:x}}| p | 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: | |||
:::* <nowiki>{{if|{{{1|}}}</nowiki><br> |then found parameter 1|else no parameter 1| ← ''extra'' "|" ''ends else-clause''<br>}} | |||
When the else-clause is indented to the next line, a newline (CR/LF) is added: | |||
:::* <nowiki>{{if|{{{1|}}}</nowiki><br> |then found parameter 1 ← ''extra newline here''<br> |else noparameter 1|<br>}} | |||
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. | |||
==See also== | ==See also== |
Revision as of 15:46, 7 September 2012
{{#invoke:Message box|mbox}}
With this template the parser functions of the ParserFunctions collection 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. Spaces still do not affect the outcome of the condition. Parameter 1 selects the if-type as "eq", "expr", "exists" or "error" (for #iferror), or empty "||" for a simple if-there (for #if). The template can be repeatedly nested, one inside the other, because the outer-most is completed before running either the then/else inner levels.
This template can be substituted.
Usage
- {{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 " q "
- {{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 "q"
- {{#iferror:{{#expr:x}}| p | q }} gives "p"
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.
See also
- m:Template:If
- Template:Ifnotempty - for simplicity and to reduce the limitations due to the expansion depth limit, this is a separate template corresponding to #if only.