Difference between revisions of "Template:Ifnotempty"

From EVE RP Wiki
Jump to: navigation, search
rp>Patrick
(copied from Template:If, to be changed into a version specific to ifnotempty to reduce expansion depth)
 
rp>Patrick
Line 1: Line 1:
<includeonly>{{{{{|safesubst:}}}{{{{{|safesubst:}}}#ifeq:{{{1}}}|eq|p1|p2}}|{{{{{|safesubst:}}}{{{{{|safesubst:}}}#ifeq:{{{2}}}|{{{3}}}|p1|p2}}|{{{4|}}}|{{{5|}}}}}|{{{{{|safesubst:}}}{{{{{|safesubst:}}}#switch:{{{{{|safesubst:}}}#if{{{1}}}:{{{2}}}|p1|p2}}|p1=p1|p2={{{{{|safesubst:}}}#ifeq:{{{1}}}|error|{{{{{|safesubst:}}}#ifeq:{{{4|+}}}|{{{4|-}}}|p2|p3}}|p2}}|if/errmsg}}|{{{3}}}|{{{4|}}}|{{{2}}}}}}}</includeonly><noinclude><div class="template-documentation" style="margin-top:1em;">{{#if:defaultnotblank|<div style="padding-bottom:3px; border-bottom:1px solid #aaa; margin-bottom:1ex;">
+
<includeonly>{{{{{|safesubst:}}}{{{{{|safesubst:}}}#if:{{{1}}}|p1|p2}}|{{{2}}}|{{{3|}}}}}</includeonly><noinclude><div class="template-documentation" style="margin-top:1em;">{{#if:defaultnotblank|<div style="padding-bottom:3px; border-bottom:1px solid #aaa; margin-bottom:1ex;">
 
<span style="font-size:150%">Documentation</span></div>}}
 
<span style="font-size:150%">Documentation</span></div>}}
 
{{Documentation subpage}}
 
{{Documentation subpage}}
With this template the parser functions of [[mw:Help:Extension:ParserFunctions|Extension:ParserFunctions]] with names starting with "#if" can be used in a way such that they do not [[Help:Newlines_and_spaces#Stripping_on_expansion|strip spaces and newlines]] from the "then" and "else" part. For evaluating the condition spaces and newlines are stripped like with ParserFunctions.
+
With this template [[mw:Help:Extension:ParserFunctions|#if]] can be used in a way such that it does not [[Help:Newlines_and_spaces#Stripping_on_expansion|strip spaces and newlines]] from the "then" and "else" part. For evaluating the condition spaces and newlines are stripped like with ParserFunctions.
  
 
==Usage==
 
==Usage==
* 4 parameters:
+
*<code><nowiki>{{ifnotempty|x| p | q }}</nowiki></code> gives "{{ifnotempty|x| p | q }}"
**<code><nowiki>{{if||x| p | q }}</nowiki></code> gives "{{if||x| p | q }}"
+
*<code><nowiki>{{ifnotempty|| p | q }}</nowiki></code> gives "{{ifnotempty|| p | q }}"
**<code><nowiki>{{if||| p | q }}</nowiki></code> gives "{{if||| p | q }}"
 
**<code><nowiki>{{if|expr|2<3| p | q }}</nowiki></code> gives "{{if|expr|2<3| p | q }}"
 
**<code><nowiki>{{if|expr|2>3| p | q }}</nowiki></code> gives "{{if|expr|2>3| p | q }}"
 
**<code><nowiki>{{if|expr|2{{=}}3| p | q }}</nowiki></code> gives "{{if|expr|2{{=}}3| p | q }}"
 
**<code><nowiki>{{if|expr||2=2=3| p | q }}</nowiki></code> gives "{{if|expr||2=2=33| p | q }}"
 
**<code><nowiki>{{if|exist| Help:Link | p | q }}</nowiki></code> gives "{{if|exist| Help:Link | p | q }}"
 
**<code><nowiki>{{if|exist| Hulp:Link | p | q }}</nowiki></code> gives "{{if|exist| Hulp:Link | p | q }}"
 
**<code><nowiki>{{if|error|<span class{{=}}"error">x</span>| p | q }}</nowiki></code> gives "{{if|error|<span class{{=}}"error">x</span>| p | q }}"
 
**<code><nowiki>{{if|error| 7 | p | q }}</nowiki></code> gives "{{if|error| 7 | p | q }}"
 
**<code><nowiki>{{if|error| 7 | p | }}</nowiki></code> gives "{{if|error| 7 | p | }}"
 
**<code><nowiki>{{if|error| 7 | p |}}</nowiki></code> gives "{{if|error| 7 | p |}}"
 
**<code><nowiki>{{if|error| 7 | p }}</nowiki></code> gives "{{if|error| 7 | p }}"
 
* 5 parameters:
 
**<code><nowiki>{{if|eq| u |u| p | q }}</nowiki></code> gives "{{if|eq| u |u| p | q }}"
 
**<code><nowiki>{{if|eq| u |v| p | q }}</nowiki></code> gives "{{if|eq| u |v| p | q }}"
 
 
 
 
Compare:
 
Compare:
* 3 parameters:
+
*<code><nowiki>{{#if:x| p | q }}</nowiki></code> gives "{{#if:x| p | q }}"
**<code><nowiki>{{#if:x| p | q }}</nowiki></code> gives "{{#if:x| p | q }}"
 
**<code><nowiki>{{#ifexpr:2<3| p | q }}</nowiki></code> gives "{{#ifexpr:2<3| p | q }}"
 
**<code><nowiki>{{#ifexist: Help:Link | p | q }}</nowiki></code> gives "{{#ifexist: Help:Link | p | q }}"
 
**<code><nowiki>{{#iferror:<span class="error">x</span>| p | q }}</nowiki></code> gives "{{#iferror:<span class="error">x</span>| p | q }}"
 
**<code><nowiki>{{#iferror: 7 | p | q }}</nowiki></code> gives "{{#iferror: 7 | p | q }}"
 
**<code><nowiki>{{#iferror: 7 | p | }}</nowiki></code> gives "{{#iferror: 7 | p | }}"
 
**<code><nowiki>{{#iferror: 7 | p |}}</nowiki></code> gives "{{#iferror: 7 | p |}}"
 
**<code><nowiki>{{#iferror: 7 | p }}</nowiki></code> gives "{{#iferror: 7 | p }}"
 
* 4 parameters:
 
**<code><nowiki>{{#ifeq: u |u| p | q }}</nowiki></code> gives "{{#ifeq: u |u| p | q }}"
 
 
 
 
Note that, unlike parser functions, templates require writing equals signs in values of unnamed parameters as <code><nowiki>{{=}}</nowiki></code>
 
Note that, unlike parser functions, templates require writing equals signs in values of unnamed parameters as <code><nowiki>{{=}}</nowiki></code>
  
 
For full substitution, simply use {{cnw|subst:}} or {{cnw|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.
 
For full substitution, simply use {{cnw|subst:}} or {{cnw|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):
 
 
*<code><nowiki>{{if|expr|1234>e3|1|0}}</nowiki></code> gives {{if|expr|1234>e3|1|0}}
 
*<code><nowiki>{{#ifexpr:1234>e3|1|0}}</nowiki></code> gives {{#ifexpr:1234>e3|1|0}}
 
  
 
==Expansion depth limit==
 
==Expansion depth limit==
Line 69: Line 37:
  
 
Condition: string not empty.
 
Condition: string not empty.
 
===#ifexist:===
 
 
Condition: page [[Help:Page existence|exists]] (applies also to special pages).
 
 
The usual case-sensitivity applies: if a page exists then also a non-canonical name for that page gives a positive result.
 
 
[[Template:Exists]] gives the same result, except that the result is positive for an interwiki link. You can also handle an interwiki link differently with [[Template:If interwiki link]].
 
 
The page A checking existence of page B is listed on Special:Whatlinkshere/B as a page which links to B.
 
 
On Wikimedia wikis there is a maximum for the number of distinct pages for which #ifexist is called on a page. A call of #ifexist after a transclusion of the same page does not require an extra database query and is therefore not counted. A call beyond the maximum returns "false". The html-source of each page shows the counted number of calls, as well as the maximum. The maximum is currently 500, see [[w:Wikipedia:Template limits#Expensive parser function calls]].
 
 
===#ifexpr:===
 
Condition: non-zero.<ref>[http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/ParserFunctions/Expr.php?view=markup Expr.php]</ref>
 
 
Empty gives false. Other strings which are not correct numerical or Boolean expressions give an error message<ref>[http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/ParserFunctions/ParserFunctions.i18n.php?view=markup ParserFunctions.i18n.php]</ref>:
 
<pre>
 
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',
 
</pre>
 
 
===#iferror:===
 
 
Condition: contains <code>class="error"</code> as attribute of <code><nowiki><strong></nowiki></code>, <code><nowiki><span></nowiki></code>, <code><nowiki><p></nowiki></code>, or <code><nowiki><div></nowiki></code> tags.<ref>[http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/ParserFunctions/ParserFunctions_body.php?view=markup ParserFunctions_body.php]</ref>
 
 
These are generated by #expr, #ifexpr, #time, #rel2abs, some other MediaWiki situations (such as recursive depth and template loop checks), and this template.
 
 
If the error condition does not apply and there is no "else" part, the function gives the input wikitext.
 
 
===#ifeq:===
 
Condition: equal as strings (case-sensitive) or numbers.<ref>#ifeq: uses the PHP comparison operator ==.[http://www.php.net/manual/en/language.operators.comparison.php]</ref>
 
 
*<code><nowiki>{{if|eq| 1e3 | 1000 | 1 | 0 }}</nowiki></code> gives "{{if|eq| 1e3 | 1000 | 1 | 0 }}".
 
*<code><nowiki>{{#ifeq: 1e3 | 1000 | 1 | 0 }}</nowiki></code> gives "{{#ifeq: 1e3 | 1000 | 1 | 0 }}".
 
 
To force a string-based comparison, add a non-numerical character to both compared arguments.
 
 
*<code><nowiki>{{if|eq| x1e3 | x1000 | 1 | 0 }}</nowiki></code> gives "{{if|eq| x1e3 | x1000 | 1 | 0 }}".
 
*<code><nowiki>{{#ifeq: x1e3 | x1000 | 1 | 0 }}</nowiki></code> gives "{{#ifeq: x1e3 | x1000 | 1 | 0 }}".
 
 
If desired, trim the arguments before adding this non-numerical character, for example with <code><nowiki>{{#if:x|..}}</nowiki></code>.
 
 
Text between <code>&lt;nowiki&gt;</code> tags is temporarily replaced by a unique code. This affects comparisons:
 
 
*<code><nowiki>{{if|eq|&lt;nowiki&gt;abc&lt;/nowiki&gt; | &lt;nowiki&gt;abc&lt;/nowiki&gt; | 1 | 0 }}</nowiki></code> gives "{{if|eq|<nowiki>abc</nowiki> | <nowiki>abc</nowiki> | 1 | 0 }}".
 
*<code><nowiki>{{#ifeq:&lt;nowiki&gt;abc&lt;/nowiki&gt; | &lt;nowiki&gt;abc&lt;/nowiki&gt; | 1 | 0 }}</nowiki></code> gives "{{#ifeq:<nowiki>abc</nowiki> | <nowiki>abc</nowiki> | 1 | 0 }}".
 
  
 
==See also==
 
==See also==
*[[Template:Sw]]
+
*[[Template:If]]
*[[Template:Iflenge]]
 
*[[w:Template:If]]
 
 
{{refs}}
 
{{refs}}
  
 
<div style="clear:both;" /></div></div></noinclude>
 
<div style="clear:both;" /></div></div></noinclude>

Revision as of 11:54, 27 July 2010

Documentation

Script error: No such module "Template translation". With this template #if can be used in a way such that it does not strip spaces and newlines from the "then" and "else" part. For evaluating the condition spaces and newlines are stripped like with ParserFunctions.

Usage

  • {{ifnotempty|x| p | q }} gives " p "
  • {{ifnotempty|| p | q }} gives " q "

Compare:

  • {{#if:x| 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 Template:Cnw or Template:Cnw. 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.

Expansion depth limit

7 levels allowed:

{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|a}}}}}}}}}}}}}} gives Template:If

{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|{{if||x|a}}}}}}}}}}}}}}}} gives Template:If

{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||a}}}}}}}}}}}}}} gives Template:If

{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||{{if||||a}}}}}}}}}}}}}}}} gives Template:If

{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|a}}}}}}}}}}}}}} gives Template:If

{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|{{if|expr|1|a}}}}}}}}}}}}}}}} gives Template:If

To allow more levels there is a separate Template:Ifexpr.

Internal technical details

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

Conditions

#if:

Condition: string not empty.

See also