Help:ParserFunctions: Difference between revisions

Content deleted Content added
expr syntax
m Reverted changes by 146.0.216.80 (talk) to last version by Sakura emad
Tag: Rollback
 
(601 intermediate revisions by more than 100 users not shown)
Line 1:
{{MovedToMediaWiki|Help:Extension:ParserFunctions}}
{{languages}}
 
This MediaWiki extension is a collection of parser functions. Parser functions typically have the syntax:
 
:<nowiki>{{</nowiki>#functionname: argument 1 | argument 2 | argument 3...}}
 
This module defines five functions at present: <tt>expr</tt>, <tt>if</tt>, <tt>ifeq</tt>, <tt>ifexpr</tt>, and <tt>switch</tt>.
 
== expr ==
 
The <tt>expr</tt> function computes mathematical expressions. The syntax is:
 
:<nowiki>{{</nowiki> #expr: ''expression'' }}
 
 
The supported operators (roughly in order of precedence) are:
:{| cellpadding="6px" border=1 style="border:1px solid #C0C0C0; border-collapse:collapse;"
! ''Operator''
! ''Operation''
! ''Example''
|-
! *
|| Multiplication
||<nowiki>{{#expr: 30 * 7}}</nowiki> = {{#expr: 30 * 7}}
|-
! / <span style="font-weight: normal">or</span> div
|| Division
||<nowiki>{{#expr: 30 / 7}}</nowiki> = {{#expr: 30 / 7}}
|-
! +
|| Addition
||<nowiki>{{#expr: 30 + 7}}</nowiki> = {{#expr: 30 + 7}}
|-
! -
|| Subtraction (or negation)
||<nowiki>{{#expr: 30 - 7}}</nowiki> = {{#expr: 30 - 7}}
|-
! mod
|| Modulo, gives the remainder of a division
||<nowiki>{{#expr: 30 mod 7}}</nowiki> = {{#expr: 30 mod 7}}
|-
! round
|| Rounds off the number on the left to the specified number<br/> of digits after the decimal place, given on the right
||<nowiki>{{#expr: 30 / 7 round 7}}</nowiki> = {{#expr: 30 / 7 round 7}}
|-
! =
|| Equality
||<nowiki>{{#expr: 30 = 7}}</nowiki> = {{#expr: 30 = 7}}
|-
! <> <span style="font-weight: normal">or</span> !=
|| Inequality
||<nowiki>{{#expr: 30 <> 7}}</nowiki> = {{#expr: 30 <> 7}}
|-
! <
|| Less than
||<nowiki>{{#expr: 30 < 7}}</nowiki> = {{#expr: 30 < 7}}
|-
! >
|| Greater than
||<nowiki>{{#expr: 30 > 7}}</nowiki> = {{#expr: 30 > 7}}
|-
! <=
|| Less than or equal to
||<nowiki>{{#expr: 30 <= 7}}</nowiki> = {{#expr: 30 <= 7}}
|-
! >=
|| Greater than or equal to
||<nowiki>{{#expr: 30 >= 7}}</nowiki> = {{#expr: 30 >= 7}}
|-
! and
|| Logical AND
||<nowiki>{{#expr: 30 and 7}}</nowiki> = {{#expr: 30 and 7}}
|-
! or
|| Logical OR
||<nowiki>{{#expr: 30 or 7}}</nowiki> = {{#expr: 30 or 7}}
|-
! not
|| Logical NOT
||<nowiki>{{#expr: not 7}}</nowiki> = {{#expr: not 7}}
|-
! ( )
|| Grouping operators
||<nowiki>{{#expr: (30 + 7) * 7 }}</nowiki> = {{#expr: (30 + 7) * 7 }}
|}
 
The boolean operators consider 0 to be false and 1 to be true. Numbers are given in decimal with "." for the decimal point. FORTRAN-style scientific notation is not supported.
 
Example:
 
<nowiki>{{ #expr: (100 - 32) / 9 * 5 round 0 }}</nowiki>
 
gives:
 
{{ #expr: (100 - 32) / 9 * 5 round 0 }}
 
which is 100&deg;F in &deg;C, rounded to the nearest whole number.
 
== if ==
 
The <tt>if</tt> function is an if-then-else construct. The syntax is:
 
<nowiki>{{</nowiki> #if: ''&lt;condition>'' | ''&lt;then text>'' | ''&lt;else text>'' }}
 
If the condition is an empty string or consists only of whitespace, then it is considered false, and the ''else text'' is returned. Otherwise, the ''then text'' is returned. The ''else text'' may be omitted, in which case the result will be blank if the condition is false.
 
An example:
<nowiki> {{Template|parameter=something}} {{Template}} {{Template|parameter=}}</nowiki>
<nowiki> | | |</nowiki>
<nowiki> | | |</nowiki>
<nowiki> | | |</nowiki>
<nowiki>{{ #if: {{{parameter|}}} | Parameter is defined. | Parameter is undefined, or empty }}</nowiki>
 
Note that the <tt>if</tt> function does '''not''' support "=" signs or mathematical expressions. <nowiki>{{#if: 1 = 2|yes|no}}</nowiki> will return "yes", because the string "1 = 2" is not blank. It is intended as an "if defined" structure. To compare strings, use <tt>ifeq</tt>. To compare numbers, use <tt>ifexpr</tt>.
 
== ifeq ==
 
<tt>ifeq</tt> compares two strings, and returns another string depending on the result of that comparison. The syntax is:
 
<nowiki>{{</nowiki> #ifeq: ''&lt;comparison text 1>'' | ''&lt;comparison text 2>'' | ''&lt;equal text>'' | ''&lt;not equal text>'' }}
 
== ifexpr ==
 
<tt>ifexpr</tt> evaluates a mathematical expression and returns one of two strings depending on the result.
 
<nowiki>{{</nowiki> #ifexpr: ''&lt;expression>'' | ''&lt;then text>'' | ''&lt;else text>'' }}
 
If the expression evaluates to zero, then the ''else text'' is returned, otherwise the ''then text'' is returned. Expression syntax is the same as for <tt>expr</tt>.
 
== switch ==
<code>switch</code> compares a single value against multiple others, returning a string if a match is found. The syntax is basically:
 
<nowiki>{{</nowiki> #switch: ''&lt;comparison value>''
| ''&lt;value<sup>1</sup>>''=''&lt;result<sup>1</sup>>''
| ''&lt;value<sup>2</sup>>''=''&lt;result<sup>2</sup>>''
| ''...''
| ''&lt;value<sup>n</sup>>''=''&lt;result<sup>n</sup>>''
| ''&lt;default result>''
}}
 
<code>switch</code> will search through each value passed until a match is found with the comparison value. When found, the result for that value is returned (the text string after the equal sign). If no match is found, but the last item has no equal sign in it, it will be returned as the default result.
 
Note that it's also possible to have "fall through" for values (reducing the need to duplicate results). For example:
 
<nowiki>{{</nowiki> #switch: ''&lt;comparison value>''
| ''&lt;value<sup>1</sup>>''
| ''&lt;value<sup>2</sup>>''
| ''&lt;value<sup>3</sup>>''=''&lt;result<sup>3</sup>>''
| ''...''
| ''&lt;value<sup>n</sup>>''=''&lt;result<sup>n</sup>>''
| ''&lt;default result>''
}}
 
Note how value<sup>1</sup> and value<sup>2</sup> contain no equal sign. If they're matched, they are given the result for value<sup>3</sup> (that is, whatever is in result<sup>3</sup>).
 
== Installation ==
 
Download both of these files and put them in a new directory called ''ParserFunctions'' in your [[Mediawiki extensions|extensions]] directory.
 
* [http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/ParserFunctions/Expr.php Expr.php]
* [http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/ParserFunctions/ParserFunctions.php ParserFunctions.php]
 
Then put the following at the end of your [[LocalSettings.php]]:
 
require_once( "$IP/extensions/ParserFunctions/ParserFunctions.php" );
 
You can also browse the code tree here:
 
* [http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/ParserFunctions/ ParserFunctions in MediaWiki SVN]
 
== External Links ==
* [http://mail.wikipedia.org/pipermail/wikitech-l/2006-April/thread.html#34685 The discussion about the ParserFunctions in the Wikitech-l list archive]
* [http://mail.wikipedia.org/pipermail/wikitech-l/2006-April/034892.html ParserFunctions trial]
*[[:en:Category:Templates using ParserFunctions]]
 
[[Category:MediaWiki extensions]]