Help talk:Extension:ParserFunctions

From MediaWiki.org
Jump to: navigation, search


Contents

#ifexist problem [edit]

I'm creating a template to redirect red links to wikipedia, here's the content of the template :

{{#ifexist:{{{1}}}|[[{{{1}}}]]|[[w:en:{{{1}}}|{{{1}}}]]}}

theorically if {{{1}}} exists output [[{{{1}}}]], if doesn't exist output [[w:en:{{{1}}}|{{{1}}}]], function seems right isn't it?

practically, it works with one word, for instance it works with

{{w|Example}}

, w being the name of the template but when it comes with spaced words such as

{{w|Test Page}}

, output is Page Test Page and link is w:en:Test

why is spacing causing issues? I don't get it, if anyone can help me, thanks.

Use {{fullurl:{{{1}}}}} rather than hardcoding the enwiki domain. The actual problem you're experiencing is that the division between the link target and the link text is placed at the first space in the expanded string, so you need to replace spaces with underscores in the first parameter; fullurl will do that for you, and also make the template portable to other wikis. Happymelon 16:20, 28 August 2011 (UTC)
No, it doesn't seem right. External links must not contain spaces. Try using {{urlencode:{{{1}}}|WIKI}} as part of the url.
{{#ifexist:{{{1}}}|[[{{{1}}}]]|[http://en.wikipedia.org/wiki/{{urlencode:{{{1}}}|WIKI}} {{{1}}}]}}

Gustronico 20:08, 28 August 2011 (UTC)

Fullurl didn't make it. So I tried the solution of Gustronico, the title works but the link doesn't, the space is replaced by a plus sign. It gives w:en:Test+Page

Edit : anchorencode works, but it won't work with special characters while urlencode can! If there's any solution it would be great.

You can use interwiki linking. I think it is standard to have them to the English Wikipedia, otherwise see Manual:Guide to setting up interwiki linking. On this site: w:Test page or Wikipedia:Test page. If for some reason you want external link style: {{fullurl:w:Test page}} gives //en.wikipedia.org/wiki/Test_page .--Patrick1 12:06, 3 September 2011 (UTC)

Did you put the |WIKI parameter in urlencode? It would return underscores for spaces:

  • {{urlencode:Test Page}} → Test+Page
  • {{urlencode:Test Page|WIKI}} → Test_Page
  • {{urlencode:Test Page|QUERY}} → Test+Page
  • {{urlencode:Test Page|PATH}} → Test%20Page

Gustronico 20:38, 3 September 2011 (UTC)

Logical OR failing?? (Nope, I just fail.) [edit]

Hello. This expression should return "EXPANSION IS TRUE", but returns "EXPANSION IS FALSE":

  • BOOLEAN ALGEBRA: {{#ifeq: {{#ifeq: 0 | 1 | 0}} OR {{#ifeq: 1 | 1 | 0}} OR {{#ifeq: 0 | 1 | 0}} | 1 | EXPANSION IS TRUE | EXPANSION IS FALSE }}:
    BOOLEAN ALGEBRA: EXPANSION IS FALSE;

Likewise, the below code should return TRUE [(1 || 0 || 0 = 1); should be TRUE] but returns FALSE:

  • SIMPLE BOOLEAN ALGEBRA: {{#ifeq: 1 OR 0 OR 0 | 1 | TRUE | FALSE }}:
    SIMPLE BOOLEAN ALGEBRA: FALSE

I'm running MediaWiki 1.16.3 with ParserFunctions (Version 1.4.0). Help! 17:39, 4 August 2011 (UTC)

I think you will need to use #ifexpr, as in {{#ifexpr: 1 OR 0 OR 0 | TRUE | FALSE }}, which returns:
TRUE
Analogously, {{#ifexpr: {{#ifeq: 0 | 1 | 1 | 0}} OR {{#ifeq: 1 | 1 | 1 | 0}} OR {{#ifeq: 0 | 1 | 1 | 0}} | TRUE | FALSE }} returns
TRUE
Helder 18:12, 4 August 2011 (UTC)
Erm. You're quite right here. How embarrassing for me; I've been looking at this for too long and completely missed this. Sorry about that, and thanks for the help! 18:34, 4 August 2011 (UTC)

#time [edit]

Why is it that the time function y only works for years before 2000? For example, {{#time: y | 2002 }} yields 08 (currently, when the year changes to 2009 presumably it will yield 09 at that point) when it should yield 02. This is very frustrating when trying to construct things utilizing dates with 2 digits. It doesn't seem to matter for 1999 and before, though. Examples:
{{#time: y | 2020 }} yields 20
{{#time: y | 2002 }} yields 02
{{#time: y | 1998 }} yields 98
{{#time: y | 1965 }} yields 65
Thanks for any help that anyone may be able to provide. I had no idea where to post this question, but hopefully this is the right spot. 24.236.101.233

Also, I edit mainly on wikipedia (sorry for not having a username here). If/when this is fixed, would it automatically update on wikipedia or not? Sorry, I didn't know how that worked. Thanks again for any info anyone might be able to provide. 24.236.101.233 09:52, 30 December 2008 (UTC)
The problem is that if you use {{#time: j F Y H:i | 2003 }}, you get 24 May 2003 00:00, like for {{#time: j F Y H:i | 1923 }} (24 May 1923 00:00). iAlex 12:56, 30 December 2008 (UTC)
You just need to make the date look more like a date. A 4 digit number staring with a 2 isn't enough. Try: {{#time: y | {{#time: F }} 2002 }} or {{#time: y | December 2002 }}. See strtotime. Splarka 21:35, 30 December 2008 (UTC)
It is possible with some minor limitations to use #time for specifying dates from -6999BCE to 6999CE. It requires some template hackery to bypass normal limitations of #time, but it is possible to do. One limitation is that there is no way of recognizing the precision the editor intended for certain dates and times. EG: if seconds were specified as 00, there is no way (that I have come up with yet) to tell if the user simply omitted the seconds because they didn't measure with that precision. For example of the lengths I had to go to in order to get this functionality, see code at {{:en:Template:start-date}}. (Note- you won't be able to see the results of the calculations from preview- the easiest way to see the UTC value emitted is to turn off CSS, eg, Opera View.Style.User mode. Look for the UTZ value in parens following the visible date-time.) -J JMesserly 20:05, 17 February 2009 (UTC)

I am wondering why the format code "z" delivers the day of the year starting with 0, deviating from the ISO-8601 standard, which counts from 1 to 365 (or 366). Someone recently used this code in the en:ISO-8601 article, giving wrong results. Question: was the definition here intentionally deviating? If so, how can the correct ISO ordinal date be displayed; if not please revise the magic word. −Woodstone 07:33, 11 June 2010 (UTC)

#switch [edit]

Quote from help: It is possible to have 'fall through' values, where several case strings return the same result string. This minimises duplication. — It doesn't seem to work for me. Instead, an empty string is returned for cases where right-hand side is emty. --Lozman 14:19, 3 January 2009 (UTC)

I fixed the description.--Patrick 10:20, 4 January 2009 (UTC)
Thanks a lot! Now it works OK. --Lozman 11:11, 4 January 2009 (UTC)

I have used #switch to return one string at {{templ|begin}} and a different one at {{templ|end}}. This works very well, however one of my strings had a leading space. This was not parsed correctly, the space was ignored. I have had to precede it with a line break for it to work. 213.126.244.228 14:36, 17 February 2009 (UTC)

subsequent cases [edit]

I wonder if there is any good reasons why subsequent cases are not supported. They would save some time and Code I think. Think of something like the following:

{{#switch:
|1=1 foo
|2=2 foo
|3=3 foo
|1
|2=1+2 foo
|1
|3=1+3 foo
}} 

--Danwe 00:26, 18 June 2009 (UTC)

They are supported. That example will work perfectly, when you specify a test string as the first parameter. It is not particularly useful, however, because an input of 1 will always match the first case encountered (the "1 foo" case). However, a statement like this:
{{#switch: {{{input}}}
 | foo = You entered 'foo'
 | bar = You entered 'bar'
 | baz
 | quok = You entered either 'baz' or 'quok'
}}
Will work perfectly. Happymelon 08:08, 18 June 2009 (UTC)
I know that this will work perfectly but this is not what I meant. I meant subsequent cases, match each "1" and not only the first "1". That would be the same behavior than in C++ for example where you can stop it with the break kommand. --Danwe 14:20, 21 June 2009 (UTC)
No, the behaviour in languages like C++ is to scroll through the cases until a match is found, then to execute all the remaining statements in the switch until a break is found. So the output of a statement like your example in C++ would be "1 foo2 foo3 foo1+2 foo1+3 foo" What you want is the output to be "1 foo1+2 foo1+3 foo", right? That's basically just string substitution. How would that be useful? Happymelon 21:24, 26 June 2009 (UTC)
This would be usefull if some cases should have the same output and the output is very long so you don't want to write it down for all the cases. --Danwe 18:48, 5 July 2009 (UTC)
Not sure whether this is exactly the thing you want, but I have a simple patch which I belief does this exact thing (I needed it), but it might nearly definitely break some other functionality: replace line 220 ( "return trim( $frame->expand( $valueNode ) );" ) with ($returnvalue .= trim( $frame->expand( $valueNode ) );") and add a line between 241 and 242 containing "if($returnvalue != "") return $returnvalue;". Lastly it might be necessary to add "$returnvalue = null;" at the begin of the function. -- David Mulder (can't login) 77.95.97.53 11:17, 6 January 2010 (UTC)

Example for first black president [edit]

Bush

The above example should change on Jan. 20, 2009. --Ed Poor 20:40, 9 January 2009 (UTC)

That won't because there is no Template:Age in days on this wiki, and even if there were, #if only checks nullity. Try something like:
{{#ifexpr: {{#time:U}} > {{#time:U| January 20 2009}}|Obama|Bush}}
Splarka 22:45, 9 January 2009 (UTC)

Using #if to call an image and adding attributes [edit]

I am trying to use #if to call an image. I don't want this image to link to its source page or anywhere. I think that I can do that by using "|link=" as an attribute but I can't get it to work. Anyone able to help?

  1. if:|[[Image:{{{Logo}}}|130px|]]
well, this:
  • {{#if:1|[[Image:Wiki.png|30px|link=]]}} > Wiki.png
  • {{#if:|[[Image:Wiki.png|30px|link=]]}} >
Works for me, but note that the link= parameter is new to version 1.14 which isn't released yet. Splarka 08:25, 15 January 2009 (UTC)


Errrrm - so does that mean that I have to wait for 1.14 and upgrade or is there a workaround? I tried it in mine and it didn't work - you can still click on the image and get through to the Image page, which is what I want to disable :( .

Only if you want to wait for the quarterly release, if you're ambitious you can Download from SVN to get the latest version. --Tlosk 10:46, 30 January 2009 (UTC)

Move to Help:ParserFunctions [edit]

I don't know if anyone else noticed, but Help:Extension: looks ridiculous. I amend that we move this page to the above link. -PatPeter Tournesol.png MediaWiki Support Team 00:23, 18 January 2009 (UTC)

I think the distinction can be helpful in that Help: applies to a default installation and Help:Extension: makes clear that the information applies to an added extension. --Tlosk 10:38, 30 January 2009 (UTC)

HTML in #IF statement branches is not rendered as html but as plain text [edit]

In this example I want to include or exclude a row of a talbe based on the presence of a passed paramter:

{{#if: {{{room}}} |
<tr>
<td></td>
<td></td>
<th>
Room {{{room}}}
</th>
<td></td>
<td></td>
</tr>
|  }}

However what happened is the html is renderes as plain text and I get a bunch of HTML code showing up on my page.

How can I prevent this?

Thanks, Pete.

See Manual:$wgUseTidy --Skizzerz 21:40, 5 February 2009 (UTC)
I recommend using wiki-markup instead of HTML:
{{#if: {{{room}}} |&#32;
{{#!: |-
|
|
! Room {{{room}}}
|
|
}}
}}
Sledged (talk) 00:28, 6 February 2009 (UTC)

Help [edit]

This code in a template:

{{#ifexpr: {{{width}}} > 300 | {{{width}}} | 300}}

produces this error.

Expression error: Unrecognized punctuation character "{".

but it still works when the template is used??? --Droll 13:31, 7 March 2009 (UTC)

See w:User:Droll/sandbox1 for template and w:User:Droll/sandbox3 for test. --Droll 15:16, 7 March 2009 (UTC)

Never mind. My bad. --Droll 21:42, 7 March 2009 (UTC)

What did you do wrong? I see the error still on your sandbox1 page. Have you tried to fix it? --Lance E Sloan 20:44, 9 March 2009 (UTC)
What I wanted to do was:
{{#if:{{{width|}}}|{{#ifexpr:{{{width|}}}>300|{{{width}}}|300}}|300}}
Thanks for asking. --droll [chat] 23:53, 13 March 2009 (UTC)


When I try to use a code like:

{{#ifexpr: 1 > 0 | true | false}}

I just get the input and no answer. I should get: true, but get {{#ifexpr: 1 > 0 | true | false}}. I think it have some to do with the setting in a file, but I have no idea how to fix it?

It is likely that the ParserFunctions extension is not installed on your wiki. Is it listed in Special:Version? Happymelon 11:50, 11 April 2009 (UTC)

Exactly. Now it works. I used the information from this site about Extension:ParserFunctions

Better Operator table [edit]

See Extension:ParserFunctions/Help/sl for a table that is much more helpful in my opinion. --droll [chat] 23:47, 13 March 2009 (UTC)

That operator table is GFDL, which means we can't use it here. If you feel like rewriting something that is similar (but not a derivative work), feel free to put it up here, but because the Help: namespace is Public Domain and the rest of the site is GFDL, we can't just copy/paste it. --Skizzerz 23:54, 13 March 2009 (UTC)
That is a shame but I understand. I will keep an translated version locally. I think some mention of operator precedence is worth mention. I assume its the same a c?. --droll [chat] 00:03, 14 March 2009 (UTC)

More versatile #titleparts? [edit]

Could the #titleparts: function be expanded to allow other separators than slashes (colons, maybe even spaces)? (Possibly having a third parameter to specify the separator, with slashes as default so as not to break its current use…) -- 87.165.184.244 02:06, 14 March 2009 (UTC)

Then it wouldn't be a titleparts function, it would be a string-splitter, which is a completely different concept. String-manipulation ParserFunctions are available in the StringFunctions extension; if you want proper string-manipulation functions, you need to get this installed on your wiki. Happymelon 08:17, 14 March 2009 (UTC)
Ah. Thanks for the quick reply! 87.165.185.6 01:27, 15 March 2009 (UTC)

#if doesnt works [edit]

On my version of wikimedia 1.14, #if option dont works. I'm printing - {{#if:test|test2|test3}} and nothing happens, text showing only as plain text

Joe.

Install the ParserFunctions extension. —Emufarmers(T|C) 21:27, 29 March 2009 (UTC)

I'm having trouble with the same thing. I have ParserFunctions extension installed (see http://wiki.accelereyes.com/wiki/index.php/Special:Version). I have a test page too (see http://wiki.accelereyes.com/wiki/index.php/TEST). What am I doing wrong? Why is it showing only as plain text? UPDATE: This error only occurs when I'm logged in. When I'm logged out, everything works fine.

You're running very old versions of MediaWiki and ParserFunctions. I don't know how they behave. —Emufarmers(T|C) 22:03, 25 January 2011 (UTC)

Rand() for #expr:? [edit]

Doesn't anyone think it make huge sense to add a Rand() function to the #expr: group of functions? Extension:Choose is definitely not applicable (all the hooks are in the wrong places...) and Time-based pseudo-randoms won't work either. I was amazed when I realized this wasn't there yet... Timeroot TalkContribsEdit Count 19:28, 30 March 2009 (UTC)

Why do you hate the cache so much? The cache loves you, the cache does everything it can for you, and it has to go to work and tell its friends "oh, I just ran into a door". Splarka 07:14, 31 March 2009 (UTC)
The original commit of ParserFunctions included a "rand" function. But I removed it, because I thought it would be confusing. People will expect it to work, which of course it won't, due to the parser cache. Random selection is best done with JavaScript. -- Tim Starling 09:09, 1 April 2009 (UTC)
I'd like to use it with subst, so if you still got the code, would you share it? --Sidcom 10:58, 1 July 2010 (UTC)

Is it possible to have a 3-tiered approach to parameters (not) passed in to templates? [edit]

Given a template that can be used in three ways like this:

{{ MyTemplate }}
{{ MyTemplate | param = }}
{{ MyTemplate | param = value }}

is it possible to have the template handle itself differently based on param not being given, param given without a value and param given with a value?

I've been looking at using {{{param}}} and {{{param|}}} to distinguish the first and second case (the third case handles well), but it doesn't seem to work; the output is the same either way.

It's especially how {{{param}}} handles itself when put into an {{if: }} I'm interested in.

Rafiki 07:57, 16 April 2009 (UTC)

  • "parameter" is {{#if:{{{parameter|undefined}}}|{{{parameter|undefined}}}|defined but empty}}
Not the usual method of using #if, but that #if will only trigger in your second case. Note that I edited your example above, because your second case was actually a different parameter, it was assigning "param" to {{{1}}}. If you really did mean that, that is a different barrel of fish. Splarka 08:16, 16 April 2009 (UTC)
On enwiki we tend to use the "logical negation" ¬ character that's found at the top-left of most keyboards. Since it's not something that anyone is ever likely to actually pass to a parameter. So you could test for your three separate conditions like this:
{{#switch: {{{param|¬}}}
  | ¬       = <!--undefined-->
  |         = <!--defined but empty-->
  |#default = <!--defined and with a value-->
}}
Does that make sense? Happymelon 10:08, 16 April 2009 (UTC)
Even if it may not make sense, it works like a charm. Many thanks for the insights! And you are, of course, right about the assignment of "param" to {{{1}}}; I didn't consider that at all. Rafiki 13:30, 16 April 2009 (UTC)

To distinguish between defined (and possibly empty) and undefined, use:

{{ #ifeq: {{{param|+}}} | {{{param|-}}} | param is defined | param is undefined }}.

Patrick 11:00, 14 July 2010 (UTC)

limits of #titleparts [edit]

There seems to be a limit for the #titleparts-function. These two lines of code should produce the same result ...:

{{#titleparts: 1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50/51/52/53/54/55/56/57/58/59/60/61/62/63/64/65/66/67/68/69/70/71/72/73/74/75/76/77/78/79/80/81/82/83/84/85/86/87/88/89/90/91/92/93/94/95/96/97/98/99/100 | 2 | 2 }}

{{#titleparts: 1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50 | 2 | 2 }}

... but only the second one seems to work correctly:

1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50/51/52/53/54/55/56/57/58/59/60/61/62/63/64/65/66/67/68/69/70/71/72/73/74/75/76/77/78/79/80/81/82/83/84/85/86/87/88/89/90/91/92/93/94/95/96/97/98/99/100

25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50

Is there a limit of string-length? Or a limit to the number of segments? And if there is a limit, (how) can I change it? I have to use #titleparts because I want to receive longer substrings like "2/3/4/5" and with #explode it is only possible to get one piece based on its position. Sorry for my lack of English. --213.39.183.144 17:39, 2 May 2009 (UTC)

The function only performs a maximum of 25 splits, so no more than 26 segments can be created:
{{#titleparts: 1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50 | 1 | 25 }} → 25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50
The string must also be less than 255 characters in length. Hope this clarifies. Happymelon 11:32, 3 May 2009 (UTC)
Thanks for the quick reply. Is it possible to change the maximum string-length and maximum number of splits? --80.171.78.168 12:12, 3 May 2009 (UTC) and why do I have a completely different IP today? --80.171.78.168 12:13, 3 May 2009 (UTC)
The number of splits is hardcoded into trunk/extensions/ParserFunctions/ParserFunctions.php (line 504); you could change it, but you'd have to remember to change it every time you updated the file. The maximum string length is essentially unavoidable: the string is converted into a Title object to check its validity (it is supposed to be a page title, after all :D), and the maximum title length is a function of the database field width. Happymelon 16:35, 3 May 2009 (UTC)
And if you want to avoid being confused (or confusing others) with a dynamic IP, why not create an account?? It takes literally ten seconds, and gives you all sorts of cool features... Happymelon 16:35, 3 May 2009 (UTC)
Again, thank you for the quick reply. Maybe it is possible to avoid the conversion to a Title object? So, it would be possible to use [[links]] and {{templates}} and other stuff within the string ... I'll try this. Greetings, --WiMu 10:16, 4 May 2009 (UTC) and where is my IP today? ... ah, it's my new account now ;-)

#ifexpr for strings [edit]

Would be great if #ifexpr would be enabled for strings as well. Imagine string comparison like {{#ifexpr: {{{text 1|}}} && {{{text 2|}}} |If both exist |If one or both don't exist/void}} --Danwe 14:48, 4 May 2009 (UTC)

And what would be the expected logical value of {{#ifexpr: one == 1 | true | false }}?? :D Happymelon 17:05, 4 May 2009 (UTC)
False. In this case we can look at the 1 as string and the strings "one" and "1" are not equal. I only don't know if we should define that {{#ifexpr: one > 1 | true | false }} is nonsense and will lead to an error message or if this has a meaning as well for example comparison of the string lenght. --Danwe 13:17, 5 May 2009 (UTC)
There is only one problem I can see: The and and or words. They shouldn't be interpreted as strings. A way to avoide that could be to set strings into quotes like {{#ifexpr: "{{{1|}}}" = "{{{2|}}}" | true | false }}. It should be possible to use and and or for strings as well and also mixed with number comparision like {{#ifexpr: "{{{1|}}}" = "{{{2|}}}" and 3 > 1 | true | false }} --Danwe 13:44, 5 May 2009 (UTC)
Hmn, with quotes, it could perhaps work, although there is still some rather wierd logic available ({{#ifexpr: "1" == 1 || | true | false }}??) We'd need to define an appropriate string→integer cast for such expressions, which should probably be the same for all strings. Casting to true makes some sense, as would casting to the binary representation, but both allow some rather odd concepts (division by a string?!?). Casting to an error avoids that problem, but makes them essentially unusable. Casting to different representations based on the calling operator could be at best described as "quirky"... Happymelon 17:15, 5 May 2009 (UTC)
I think it's not neccessary to compare "1" and 1, sting and non string. Because the template programmer knows what he want's to compare he should know at least whether he wants to compare strings or numbers. And if you have a string with unknown content which perhaps could be a number it doesn't matter because it could be a real string as well which you must compare as string anyway. And casting integer into string is no problem in the template, the quotes do it so it's save in any case as long the user won't forget the quotes. --Danwe 17:40, 5 May 2009 (UTC)
Casting integers to strings for comparison is a good idea, but has its own set of problems. What does {{#expr: "(1 and 2)" == (1 and 2) }} evaluate to? Or do you mean only explicit casts (using quotes) would avoid an error? That's more tenable, but what if the input strings contain quotes themselves? Don't even think about suggesting we escape them... :D Happymelon 22:11, 5 May 2009 (UTC)
"(1 and 2)" is a string, not a logical comparision which is converted into string after the comparision. You can write: 1 and 1 and "abc" because with and and or you only compare boolean results. But your last point was one I never thought about. What if there are user defined quotes inside the quotes... i agree, escaping them won't work. " inside of ' quotes could be possible but then we have the same problem with the ' quotes... honestly I have no idea yet --Danwe 18:42, 6 May 2009 (UTC)

Request: new flag to force date interpretation in #time [edit]

Is it possible to add a flag (may be xd) to force date interpretation of ambiguous inputs like four-digit and six-digit numbers, giving an error when that number is not a valid date? It should be a toggled flag like xN and it should work as follows:

  • {{#time: xdY | 2006 }}2006 instead of 2006 Fixed since rev:86805 24 April 2011
  • {{#time: xdF Y | 200611 }}November 2006 instead of May 2013
  • {{#time: xdY m d H:i:s | 200615 }}Error instead of 2013 05 24 20:06:15
  • {{#time: xdY m d H:i:s | 199804 }}1998 04 01 00:00:00 instead of Error: Invalid time.
  • {{#time: xdY m d H:i:s | 1998 }}1998 01 01 00:00:00 instead of 1998 05 24 00:00:00

Feel free to correct my English. Gustronico 00:42, 21 May 2009 (UTC)

Well, don't waste your time. I've solved the problem with {{padright: «ambiguous number» | 8 | 01 }}
Note: #time: function is omitted for simplicity. Refer to cases above.
  • {{padright: 2006 | 8 | 01 }} → 20060101 → 2006
  • {{padright: 200611 | 8 | 01 }} → 20061101 → November 2006
  • {{padright: 200615 | 8 | 01 }} → 20061501 → Error: Invalid time.
  • {{padright: 199804 | 8 | 01 }} → 19980401 → 1998 04 01 00:00:00
  • {{padright: 1998 | 8 | 01 }} → 19980101 → 1998 01 01 00:00:00
Nevertheless I think that it would be a usefull flag. Gustronico 16:08, 21 May 2009 (UTC)

Division [edit]

How much is:

  • {{#expr: 60 / 2.54}} 23.622047244094
  • {{#expr: 60 /div 2.54}} Expression error: Unexpected div operator.
  • {{#expr: 60 div 2.54}} 23.622047244094

There seems to be an error in the info box which lists /div as a binary operator. Shouldn't that be / div with a space between the slash and the letters? ----Ed Poor 21:57, 25 May 2009 (UTC)

Yes. Happymelon 22:16, 25 May 2009 (UTC)

Rounding [edit]

Is all rounding to the nearest integer, or what?

{{#expr: 123.456789 round 2}} = 123.46

Where is this documented? --Ed Poor 22:03, 25 May 2009 (UTC)

meta:Help:Calculation, hasn't been moved over from meta yet. If you want to write a PD version here, we'd be very grateful! Happymelon 22:17, 25 May 2009 (UTC)

Import from StringFunctions [edit]

The features of StringFunctions have been imported as of r50997 (probably MW 1.16). The documentation will need to be merged when it is released. GreenReaper 17:35, 31 May 2009 (UTC)

Don't hold your breath on it surviving to a release... Splarka 07:12, 1 June 2009 (UTC)
That's what I was going to say: I want to see it get through code review before I put any work into documenting it. The documentation can't be "merged" because they're incompatible licenses (Help: is public domain), so it'll have to be written from scratch. Happymelon 08:53, 1 June 2009 (UTC)
Well if someone wants to point out terrible flaws, please do. The code was almost totally rewritten from the StringFunctions implementation (which was just plain terrible in some places). In the meantime, the bastardized string manipulation templates are now being used on >20,000 enwiki pages and that will almost certainly continue growing until we either have real string functions or Brion changes his mind and kills off the padleft: abuse that made those possible. In the mean time, if someone does start working on this documentation please ping me and I'll certainly check it for accuracy and make sure it covers all the appropriate quirks. Dragons flight 21:33, 1 June 2009 (UTC)
Presumably we eventually need Help:Extension:StringFunctions anyway, so I guess we might as well start. The way the string function implementations on enwiki have grown is a very clear sign of just how valuable these methods are. But I'm not breaking out the champagne until I see {{#len:Foo}} as "3" right here.
Incidentally, do you know why Wikimedia's MW is lagging so far behind SVN trunk? We're 2,500 revisions behind now... :( Happymelon 22:19, 1 June 2009 (UTC)
Once Upon A Time, the developers were told that some simple ParserFunctions would reduce the instances of ugly {{qif}} type templates and simplify the codebase of templates, and make everyone live happily ever after. They initially and very briefly made code simpler, but then made templates a thousand times more complex (see any template on en.wp).
Adding StringFunctions will only add to the insane complexity, and turn Wikimedia's wikicode into a natural language parsing system, making templates almost impossible for anyone to edit safely, and giving the servers even more very inefficient overhead. Certain developers absolutely do not want this, as this would only make things more complex in the long run, and more system intensive. They seem to prefer playing a "But this isn't up to snuff" game on bugzilla. If you want a more frank admission of the reasoning, please see This log starting at about [16:45:35]. This almost certainly will be reverted (again) before scap, and if not, domas will probably come along later and cripple the features after tracing the crazy profiling spikes. My guess anyway.
Also, brion has been very distracted for about 7 weeks, what with dev meetups and getting hitched and all. Splarka 07:23, 2 June 2009 (UTC)
Hmm, seems Brion's actually more amenable to the idea than Tim; and it seems to be a fairly even split amongst the senior devs. As I said, I'm definitely keeping the champagne in the bottle for now.
I didn't know Brion got hooked up! Congrats to him. Is there more to read into [12:01:49] <werdna> he's also not back permanently, I think he's heading somewhere?? Is that just his honeymoon or is our venerable leader leaving us?? :S Happymelon 09:21, 2 June 2009 (UTC)
Last week was part of his honeymoon, as I understand it. Unless there is evidence to the contrary, I'd just assume he was off having a good time. I think you are correct that Brion is generally supportive of StringFunctions in concept, as long as the implementation is reasonable. The past implementations had serious flaws that generally made them unsuitable for use at the WMF scale. I'm hoping that the new version is technically acceptable, so the only concern is more of the "do we want this variety", and I think the community can have a significant voice in that. Dragons flight 23:13, 2 June 2009 (UTC)

(Undent) See rev:51497 for latest developments. I still think you should wait to see if it makes it to a release before going through the trouble and clutter of adding them here. Aside, this would mean the deprecation of the StringFunctions extension... but still, the (totally obvious) main goal of sneeking these in to Wikimedia is as yet unfulfilled. Splarka 12:24, 5 June 2009 (UTC)

I am still failing to see why these two extensions have been merged, except as you say as a broad-daylight attempt to sneak them onto WMF as part of normal code review/scap, rather than an explicit shell request. Which Tim has now just foiled. The two function sets are radically different, and yes, enabling them is as much a philosophical/ideological step as a technical one. Either way, nothing is going to happen without Brion's say-so. Happymelon 15:32, 5 June 2009 (UTC)
I'm not actually sure either (and I'm the one that merged them!). Comments at bugzilla:6455 favored such a merge, as did devs I spoke to privately before doing it. I don't "think" the intent was merely to backdoor them in (it certainly wasn't my intent anyway, and it should be obvious to any reasonable person that this would immediately draw discussion and hence couldn't actually be accomplished as some secret plot). Personally, I would have been just as happy to implement it as a separate extensions, and even think that seems more natural. Dragons flight 02:29, 6 June 2009 (UTC)

Using {{time}} in {{switch}}? [edit]

I want to show diffent content each day, for example: {{ #switch: {{#time: dmY}} | 10062009 = Content 1 | 11062009 = Content 2 }}

But every time i save the template the code is changed and does not work! --80.228.185.14 20:19, 10 June 2009 (UTC)

It is impossible for anyone here to help you with only that desciption of the problem. How does "the code change"? What does it do instead of work? The more information you provide, the more likely we are to be able to solve your problem. Happymelon 22:54, 11 June 2009 (UTC)
Hi Happy-melon, thanks for your reply. The code changes to the following {{ #switch: }}{{ #time: dmY}} | 10062009 = Content 1 | 11062009 = Content 2 }}--80.228.182.220 15:32, 12 June 2009 (UTC)
That is extremely odd. Can you provide a url to the wiki where you're experiencing this problem? Happymelon 16:31, 12 June 2009 (UTC)
Just look here: http://gzsz-wiki.de/index.php/Sandkasten. The code only changes if you save a page. in preview-mode the code works. maybe a plugin causes the changes? --80.228.183.126 07:40, 13 June 2009 (UTC)
To be honest, it's probably something wrong with one or more of the wierd-and-wonderful extensions that are installed there, most of which are not WMF-stable. What happens if you try and remove just one of the extra braces? Happymelon 08:12, 13 June 2009 (UTC)
If i remove just one after saving there are three of the braces. ;-) I'll try to deactivate the plugins to check if of of them produces the error. --80.228.183.25 19:55, 13 June 2009 (UTC)
Yeah, the errors suffer from the autolink-plugin. i deactivated it and now it works. --80.228.182.105 09:42, 16 June 2009 (UTC)

Switch and Parameters/Arguments [edit]

Is there anyway to use switch with a passed parameter/Argument? Something like:

{{#switch: {{{1}}} | case1 = blah | case2 = duh | default whatever}} --71.187.221.128 23:13, 13 June 2009 (UTC)
Exactly like that. Splarka 07:36, 14 June 2009 (UTC)

Setting timezone [edit]

Hi.

Shouldn't this give different results?

{{#time:Y-m-d H:i:s|2009-07-30T22:00:00+02:00}}
{{#time:Y-m-d H:i:s|2009-07-30T22:00:00+00:00}}

Just as does this:

<?php
        echo date('Y-m-d H:i:s', strtotime('2009-07-30T22:00:00+02:00')) . "<br />";
        echo date('Y-m-d H:i:s', strtotime('2009-07-30T22:00:00+00:00')) . "<br />";
?>

--Nux 23:36, 30 July 2009 (UTC)

Parameter with #expr [edit]

I have a parameter in my template, say {{{number}}}. How can I use it with #expr so that when {{{number}}}=20

{{#expr: {{{number}}}-10}}

produces 10? All I get is the error: Expression error: Unrecognised punctuation character "{"

It will work when transcluded. If you want it to work on the template page, give it a fallback default like {{{number|0}}}, or wrap it in noinclude tags. Splarka 07:19, 5 August 2009 (UTC)
That seems to do the trick. Thank you!

This worked for me too:

{{#if:{{ {{{1|0}}}|out=found|}}|  {{ {{{1|0}}}|out=found|}} }}

Thank you. Errectstapler 21:40, 12 May 2011 (UTC)

Internal link in Template - not working [edit]

I created a following template, modeled after w:en:Template:Missing_information. I wanted to add a parameter "anchor" so I could jump to a section on the talk page. But when I try to use it, I get garbage instead of an internal link like Talk:Pagename#Section

{{ambox
| type  = content
| text  = '''To do:''' {{{1}}} See [[:{{NAMESPACE}} talk:{{PAGENAME}}{{#if: {{{anchor|}}}|#{{{anchor}}}}}|talk page]] for  additional comments (if any). {{#if:{{{date|}}}|<small>''({{{date}}})''</small>}}
}}

Expensive parser functions [edit]

How can the number be changes?--Launchballer 19:56, 11 September 2009 (UTC)

As it says very clearly on this help page, by changing $wgExpensiveParserFunctionLimit. See Manual:$wgExpensiveParserFunctionLimit for more details. Happymelon 21:31, 11 September 2009 (UTC)

Conditional statements inside math functions [edit]

I am trying to create a template while using a conditional statement inside of a math tag (<math>...</math>), but I'm having problems. It seems like the math tags are running before the conditional statements, but after receiving the other parameter values from the template. Here's the code, which can be found at Template:DentalFormula:

<math alt="Upper: {{{upper}}} / Lower: {{{lower}}}{{#if: {{{total|}}} |, Total teeth = {{{total}}}|}}">\tfrac{ {{{upper}}}}{ {{{lower}}}}{{#if: {{{total|}}} | \times 2 = {{{total}}}|}}</math>

This yields the error: "Failed to parse (lexing error): \tfrac{ {{{upper}}}}{ {{{lower}}}}{{#if: {{{total|}}} | \times 2 = {{{total}}}|}}"

Am I doing something wrong? Is there any way to fix this? –Visionholder 03:47, 16 September 2009 (UTC)

You can't use template parameters directly in a parser extension tag. You have to convert it to a parser function with #tag. Splarka 07:03, 16 September 2009 (UTC)
You absolutely rock! I never would have figured that out. Thank you! –Visionholder 08:41, 16 September 2009 (UTC)

Bug? [edit]

Is this the expected behaviour?

<math>\unknownname</math>Failed to parse (unknown function\unknownname): \unknownname
{{#iferror: <math>\unknownname</math> | error | correct }}correct

I mean, if the math formula returns an error, should #iferror return "error"? Helder 17:18, 29 November 2009 (UTC)

It's not ideal, but it is expected. Extension tags like <math> are processed after parser functions like iferror, so when the function is evaluated, the input string is just "<math>\unknownname</math>", not the expanded HTML with error class. Other tags, like <timeline>, <ref>, etc, behave the same way. Happymelon 17:55, 29 November 2009 (UTC)
Is this order the reason for this too?
{{#ifeq:{{:Special:PrefixIndex/MediaWiki talk:}}|{{:Special:PrefixIndex/MediaWiki talk:}}|yes|no}}no
(I was thinking about the possibility of using something like this to watch the creation of discussion of MediaWiki messages...) Helder 19:04, 29 November 2009 (UTC)
Essentially, yes. <tt>{{:Special:PrefixIndex/MediaWiki talk:}} is replaced by a strip marker (a placeholder that tells the parser that more complicated content will later be inserted), which is a unique reference. Compare also {{#ifeq:[[foo]]|[[foo]]|yes|no}}no , because links are also replaced by strip markers. Happymelon 15:41, 30 November 2009 (UTC)
Thanks for the information! =) Helder 17:08, 30 November 2009 (UTC)
The example above, {{#ifeq:[[foo]]|[[foo]]|yes|no}}no , is incorrect because the evaluated part of it is missing an opening square bracket in the second [foo]]. With the bracket: {{#ifeq:[[foo]]|[[foo]]|yes|no}}yes. Hamilton Abreu 20:47, 29 January 2010 (UTC)

#titleparts warning [edit]

I added a warning about #titleparts, coming from a very difficult bug I discovered yesterday into a complex template I wrote. The functiong capitalizes the first substring only!--Alex brollo 11:33, 2 January 2010 (UTC)

Repeating function [edit]

Is there a repeating Parser Function?--Περίεργος 12:03, 29 January 2010 (UTC)

Not in this extension. See Extension:Loops and Extension:LoopFunctions. Splarka 09:21, 30 January 2010 (UTC)

Using tabs and newlines in the string searching functions [edit]

I have a string something like "a a[ tab ]b" where "[ tab ]" represents the tab character. I would like to find the position within the string of "a[ tab ]". Unfortunately, the #pos function seems to strip the tab from the search term, giving 0 instead of 2. Here's what I've tried:

{{#pos:a a b|a     }}
{{#pos:a a      b|a{{1x|        }}}}

Unfortunately, all of these find the first and not the second a. I'd also like to know how to do this with a newline instead of a tab. Thanks. Eric304 08:50, 7 February 2010 (UTC)

Whitespace not stripping [edit]

I'm not entirely certain my problem is related to this extension, but it started when I was editing a newly template, adding the #switch function of this extension to it and is affecting other templates, also calling the switch function. I had created a new template in which, within the results of each possible variable for the #switch function, I had invoked "nowiki" tags around an area of text. Since then, whitespace is not being stripped from any of my templates using the #switch function, resulting in some undesirable formatting. on the pages using those templates. The first thing I tried doing was replacing all the files for this extension with fresh copies, but that didn't work. After reading about whitespace and "nowiki" tags at the bottom of this hep page, I tried to undo the changes to the template, but when that had no effect, I deleted the template from the table in PHPmyAdmin. Still there is no effect. The only way it seems to restore the formatting on the pages calling the other extensions is to manually remove all the excess whitespace from the templates, which I tried and succeeded with one, but is not desirable as it makes the template difficult to interpret when I have to go back and edit these templates down the road. Does anyone know how to restore the automatic whitespace stripping functionality? --Ds093 14:52, 21 February 2010 (UTC)

I have a similar issue with variables on the top of a page in a wiki;
{{#vardefine:CC1|background:#CB0000}} <!-- First Character Menu Color -->
{{#vardefine:CC2|background:#AE0000}}
{{#vardefine:CC3|background:#910000}}
{{#vardefine:CC4|background:#740000}}
{{#vardefine:CC5|background:#570000}}
{{#vardefine:CC6|background:#3A0000}}
{{#vardefine:CC7|background:#1D0000}}
{{#vardefine:CC8|background:#000000}} <!-- 8th Character Menu Color -->
{{#vardefine:CC9|background:#000000}} <!-- This Character's Menu Color -->
{{#vardefine:charname|John Carter}}
{{#vardefine:FirstPro|Warrior}}
{{#vardefine:SeconPro|Human}}
{{#vardefine:Level|1}}
{{#vardefine:Goals|
* Goal 1 
* Goal 2 
* Goal 3}}

These variables seem to cause white space at the top of my page and the only way to rid myself of it is to set the style margin into negative values. (such as: style="margin-top:-200px;) I hope that helps. I am still looking for another solution.

The question is a bit off-topic here, but it is natural that newlines between calls give blank lines. You can remove them, or put them in comments like this:
{{#vardefine:CC1|background:#CB0000}}<!-- First Character Menu Color 
-->{{#vardefine:CC2|background:#AE0000}}<!--
-->{{#vardefine:CC3|background:#910000}}
Patrick1 (talk) 11:59, 4 March 2012 (UTC)

#titleparts with PAGENAME [edit]

i tried {{#titleparts:{{PAGENAME}}|1}} to get only the first title part of the page, but it somehow didn't work out. does anyone know why? 87.177.222.84

You probably don't have subpages enabled in the namespace you're trying to use the function in. Happymelon 23:00, 1 March 2010 (UTC)

Switch default [edit]

Guys, I think the implied default is broken. I had to use the explicit "#default". Check it out, in case I'm right. --JokerXtreme 12:32, 4 March 2010 (UTC)

Works for me. I bet your implied default contains an equals sign. You must use "#default" if the implied default contains an equals sign, as this is normal behavior (the first equals is always the delimiter). Splarka 08:06, 5 March 2010 (UTC)
I think that the documentation should be updated to reflect the difference between the two default variants. I will leave this to others as I'm too frightened to even think about modifying this page. Neil Smithline (talk) 21:53, 17 February 2013 (UTC)
I always use #default= regardless because it makes it easier for myself a week up the road and other editors to see what's what. Also, we use {{=}} and {{!}} any time we are inside of any magic word, parser function, string function, etc... It's just safer. -- ShoeMaker   ( Contributions Message )   14:12, 18 February 2013 (UTC)

#denamespace [edit]

Is there a way to convert a page name to its name without the namespace? For example:

Category:Extensions                      ---->    Extensions
Help talk:Extension:ParserFunctions      ---->    Extension:ParserFunctions

Unfortunately, "#denamespace" isn't an actual function, so I was wondering if there were ways to do this. Regards, Arbitrarily0 (talk) 00:51, 6 March 2010 (UTC)

Please refer to Help:Magic words#Page names. Hamilton Abreu 23:25, 8 March 2010 (UTC)
Ah, thanks! I didn't think of using this. For those of whom it interests, {{BASEPAGENAME}} can be used to do this. For example {{BASEPAGENAME:Help talk:Extension:ParserFunctions}} yields Extension:ParserFunctions. Cheers, Arbitrarily0 (talk) 16:28, 9 March 2010 (UTC)

#time utc offset bug [edit]

  • {{#time:Y-m-d H:i:s|2010-03-11T13:30:00+00:00}} result 2010-03-11 13:30:00
  • {{#time:Y-m-d H:i:s|2010-03-11T13:30:00+02:00}} result 2010-03-11 13:30:00

TimeCurrency 18:28, 12 March 2010 (UTC)

Splitting [edit]

is there something that would split a number up. link if i had 1923 and make it 1, 9, 2, 3 or something? Gman124 20:27, 16 March 2010 (UTC)

Use "y" to get 23. For everything else, there's Extension:StringFunctions. :) --Church of emacs talk · contrib 20:31, 16 March 2010 (UTC)
ok thank you.—The preceding unsigned comment was added by Gman124 (talkcontribs) . Please sign your posts with ~~~~!
See also m:Template:Digit.--Patrick 09:59, 12 September 2010 (UTC)

new section [edit]

when i use these functions on my language wiki, the time always comes in the my language's number form so these always mess up. So, is there a way to make the time come in english numbers or make these or make these functions right?

like for the following:

{{#expr:{{{year|{{CURRENTYEAR}}}}}-1}} it should give 2012

but on my wiki it gives an error because {{CURRENTYEAR}} giving ੨੦੧੦ as year value so get error like

Expression error: Unrecognized punctuation character "�".

so is the expr thing expecting 2010 instead? and how would that be fixed. so that it takes the value in my language. or is there a way to turn number from one lagunage version to another? Gman124 21:13, 16 March 2010 (UTC)

Check Username [edit]

Is there a way (a magic word maybe?) to make an if-function that checks the username? Like: if username=thisname show that.

Yes, it's possible to display something according to the last page editor, with {{#ifeq:{{REVISIONUSER}}|JackPotte|Validated|Not validated}} (Not validated), but for more you'll need JavaScript. JackPotte 18:40, 11 May 2010 (UTC)

titleparts misinterprets underscores [edit]

{{#titleparts:foo/foo/bar_baz||3}} = bar baz, but it should be bar_baz. This is buggy behaviour when handling with URLs. A fix requires ugly double replacing. --Subfader 13:50, 18 May 2010 (UTC)

Would using:
{{urlencode:{{#titleparts:foo/foo/bar_baz||3}}}} = bar+baz
{{anchorencode:{{#titleparts:foo/foo/bar_baz||3}}}} = bar_baz
depending on where in the url you're going to place it, be of any help? Hamilton Abreu 15:06, 18 May 2010 (UTC)
I know how to fix it, I just wanted to report it. Dunno if this is intended behaviour tho. --Subfader 12:21, 19 May 2010 (UTC)

Another bug: {{#titleparts:foo/bar_||2}} = bar, but it should be bar_. I also know how to fix this but it's just too much of "smart" behaviour :( --Subfader 23:13, 19 May 2010 (UTC)

Ok, I see. But reporting it here will yield no results. Please report all MediaWiki bugs in bugzilla. Hamilton Abreu 01:42, 20 May 2010 (UTC)
Done. I wasn't sure if all this is intended behaviour tho. --Subfader 12:11, 20 May 2010 (UTC)
To be honest, neither am I. Hamilton Abreu 13:59, 20 May 2010 (UTC)

switch Foo & Bar!=... [edit]

I want to use #switch for page names, but it doesn't work for me when the title uses & (and !) ([[:Category:Foo & Bar!]] in this case):

{{#switch:{{PAGENAME}}
|Foo & Bar!=...
}}

I tried using |Foo {{&}} Bar{{!}}=Bar, but that doesn't do the trick either. Anyone knows a workaround? It works with {{PAGENAMEE}} and Foo_%26_Bar! but actually I want to keep using {{PAGENAME}}. --Subfader 17:28, 5 June 2010 (UTC)

for & " ' you have to use &amp; &quot; &#39;. These are (currently) the only three characters allowed in titles that need to be escaped for XML/html parsing. A quick way to see the raw comparison is to use Special:ExpandTemplates which gives you the 'result' pre-rendering, eg here. Splarka 07:29, 6 June 2010 (UTC)
Aaah. Thanks for enlighting me :) --Subfader 20:02, 6 June 2010 (UTC)


using #time in other languages [edit]

I tried to use #time word for tamil but its throwing error. {{PAGENAME}} gives something like ஜூன் 7 (=June 7) passing {{#time|ஜூன் 7}} throws error. How can i achieve it? thanks -Mahir78 07:09, 13 June 2010 (UTC)

#time limit [edit]

There seems to be a limit on the number of occurrences of #time in a single wiki page. This was observed on the Wikipedia page w:en:List_of_oil_spills (see the changes in this difference: http://en.wikipedia.org/w/index.php?title=List_of_oil_spills&action=historysubmit&diff=368179588&oldid=368005766).

The page contains a huge sortable table of dates, using the {{dts}} template, which was using #time to help parse its input parameters (in the form yyyy-mm-dd). After the table grew to a certain size, we started seeing "Expression error: Unexpected < operator" cropping up in {{cite}} templates down in the reference section. (This error wasn't very helpful, so we couldn't figure it out.) Eventually the table grew big enough that we got a "Error: too many #time calls" in one cell, which was more helpful. This enabled us to fix the problem by changing the {{dts}} input parameters to the form yyyy|mm|dd, which doesn't use #time.

I don't know why there's a limit on #time, but I suggest discovering the source of the limitation and posting it on this ParserFunctions page, to help others who might encounter this problem.

(Wikipedia user: Johnson487682) 12.199.176.12 14:28, 15 June 2010 (UTC)

Done.--Patrick 10:04, 12 September 2010 (UTC)

Understanding this page for non-computer programmers [edit]

In my edits, I have tried to address what this I feel this page needs:

  1. Real life examples. {{#switch: baz | foo = Foo | baz = Baz | Bar }} → Baz is too complex, and is not as good as real life examples.
  2. Explanation of the practical uses of these parsers. What can be done with these parsers?

I added [1] to the switch section in an attempt to make this page easier to understand. Adamtheclown 17:14, 1 July 2010 (UTC)

At the very least, such examples shouldn't be enwiki-centric, because not so many people in the world know what the hell infobox is. Max Semenik 17:23, 1 July 2010 (UTC)
Hi Adamtheclown. I understand you put a lot of work into creating this material and recognized so in the initial rollback comment. Your continued contribution to improve the documentation is welcome, but I believe this was not the case here. Could you please consider the following:
  • Examples should as simple, clear and concise as possible. Simply put, the examples used are confusing; altogether, they use two templates and three pages to attempt to illustrate a basic use of #switch. It took me several reads just to understand what they are trying to demonstrate. They also require an understanding of templates, template parameters, named template parameters, wikitable formatting and magic words just to read them. None of this helps understand, nor is central, to what the function does.
  • Real life examples go at the bottom. It is illogical to interrupt a description of the function and present to readers three complex examples of its use, before that function has been described.
  • Being in the wrong place and not being simple, clear and concise, these examples actually have the opposite effect to what you intend: they are unhelpful and distract and confuse the reader.
Now, generally I agree with your key points of real life examples and practical uses. But surely you know that if we were to do so for all functions on this page in the manner of these examples, the page would more than duplicate in size and become dense and probably unreadable.
At all times, we're trying to reach a balance between two extremes, those of being clear and of being real, and it's difficult. But if you're gonna fail one way or the other, you should fail in the latter, not in the former. For example, please consider that users usually come here in order to understand a real life example they found elsewhere.
And it's natural that at times different people will have different views on this. But if you understand the points above, you'll surely understand that, for the benefit of the page as a whole, the edit should be rolled back again. However, I will leave it up to you to please do so. Hamilton Abreu 19:13, 1 July 2010 (UTC)
Thank you for your comments, is there anyway to make a real life example which is easier to understand? A middle ground between my "confusing" real life examples and {{#switch: baz | foo = Foo | baz = Baz | Bar }} → Baz which to me, is much more confusing.
Imagine yourself being first introduced to parser functions, with little computer programming experience, is the current format descriptive enough to teach you how to learn? For me, the answer was no. I had to go to wikipedia and look for examples, and ask on village pump how to use these functions.
RE: "It is illogical to interrupt a description of the function and present to readers three complex examples of its use, before that function has been described."
By bottom do you mean bottom of the page?
My attempt at improving this page, was simply an attempt, and was not the ideal examples, I just saw the page as being to complex right now. Lets work together to come up with better examples, which makes the page easier to understand. Examples could maybe be collapsed in each section.Adamtheclown 15:12, 2 July 2010 (UTC)

Examples of use [edit]

How can examples of switch be written more clearly? Feel free to completely start from scratch.

Example

Usually to update the information on a wiki, an editor would have to update manually both pages. This results in information on one page being different on another page. But by using #switch, all template information is kept in one central location.

The page "Fruits" has a list of food in a template. Each one of these food items on the "Fruits" page have their own page, with templates in a different format. So the template on the page "Apple" has the same template information as the page "Fruits".

Coding example
Template:apple Fruit Apple
Coding

{{#switch:{{{out}}}
| type = Northern United States
| color= red
| taste= delicious
}}

{|
|[[File:apple.png|20px]]
|type = {{apple|out=type}}
|color = {{apple|out=color}}
|taste = {{apple|out=taste}}
|}

{{
|type = {{apple|out=type}}
|color = {{apple|out=color}}
}}

Result

(The picture "apple.png")
Northern United States
Red
delicious

Northern United States
Red

Adding information from several templates into one template

Switch will also allow an editor to add information from several templates which have the {{#switch:{{{out}}} coding into one template.

Example

For example, if an editor editing the page "Fruit" wanted to add information in one section from:

  1. "Template:apple" (type: Northern United States) and
  2. "Template:grapes" (color: purple)

...and both of these templates used {{#switch:{{{out}}}, the editor could type:

Coding Result

{|
|[[File:apple.png|20px]]
|type = {{apple|out=type}}
|color = {{grapes|out=color}}
|}

(The picture "apple.png")

Northern United States
purple

Pages with the same name as the template

If the page has the same name as the template, the editor can use {{PAGENAME}}

Example

In the example of template:apple, an editor could add the following to the "apple" page (See m:Help:Magic words for more details):

Coding on "Apple" Result

{{Infobox
|type = {{{{PAGENAME|out=type}}
|color = {{PAGENAME|out=color}}
}}

Northern United States

red

Uses [edit]

Each section would benefit from a short blurb on what the parser function can accomplish.

I retained this, one of many uses for switch:

#switch allows an editor to add information in one template and this information will be visible in several other templates which all have different formatting. Ref: Embedded template, Wikipedia:Village Pump (technical), (24 June 2010).

Adamtheclown 15:17, 2 July 2010 (UTC)


Adam, thanks for this. Answering your points in turn:
  • Personally, I find the use of "foo", "bar", "baz" and "quok" rather distracting. My brain reads "foo" and "bar" and proceeds to shuts off, requiring a reset. But they seem to be deeeply ingrained in the minds of programmers, coming from early C times, possibly earlier. The /pt version of the PD help uses examples like:
{{#switch: 1 | 1 = one | 2 = two | other }} → one
{{#switch: 2 | 1 = one | 2 = two | other }} → two
{{#switch: 3 | 1 = one | 2 = two | other }} → other
Don't know how you feel about those, but to me they're as simple as one can get, while fully illustrating what #switch does without distracting the user with anything else.
  • I apologise for failing to qualify "bottom". I meant the end of the #switch section. If you go there, you'll notice a note that already directs the reader to two real life examples, the first meant to be simple and the second more complex. How do you feel about those?
  • This is an interesting challenge, which I cannot raise up to for lack of availability. But the challenge rests on you. I can, however, provide a couple of suggestions.
  1. Being real, they should point to real implementations, as is done in the two just mentioned.
  2. Because they may change in future, we could use permanent links. Building real life examples in this page will cause clutter and is prone to natural errors (your examples contained some syntax innacuracies such as {{{{PAGENAME|out=type}}, which would be avoided).
  3. Because this is an opportunity for plugs, we could restrict them to wikimedia wikis. Perhaps just to mediawiki.org, since we already have template {{mediawiki}} to connect here from the PD help pages.
  4. We could perhaps not just point users to them (as is done in the section), but also explain what #switch is being used for, in them.
  5. There may be even better or simpler examples out there. The challenge is in finding them.
  6. Rather than a note, we could perhaps agree on a bottom section for every function with up to 4-5 examples, or something like that.
Hamilton Abreu 13:27, 4 July 2010 (UTC)
really excellent points! Errectstapler 21:38, 13 May 2011 (UTC)

more examples and slight rewrite [edit]

Added and clarified with the following:

This parser function compares two strings and determines whether they are identical.

{{#ifeq: string 1 | string 2 | value if identical | value if different }}
text

The comparison is made as text; this comparison is case sensitive:

{{#ifeq: foo | bar | yes | no}} → Is foo and bar the same text? Answer: no
{{#ifeq: foo | Foo | yes | no}} → Is foo and Foo the same text? Answer: no
{{#ifeq: "01" | "1" | yes | no}} → Is "01" and "1" the same text? Answer: no (compare to same example below, without quotes)
{{#ifeq: 10^3 | 1000 | yes | no}} → Is 10^3 and 1000 the same text? Answer: no (compare to same example below)
number

If both strings are valid numerical values, the strings are compared numerically:

{{#ifeq: 01 | 1 | yes | no}}yes
{{#ifeq: 0 | -0 | yes | no}}yes
{{#ifeq: 1e3 | 1000 | yes | no}}yes
{{#ifeq: {{#expr:10^3}} | 1000 | yes | no}}yes
parameter

A sting can be a parameter, which is defined somewhere else:

{{#ifeq:{{{1}}}|foo| yes | no }} → If the parameter {{{1}}} equals foo, then the #ifeq will display yes, otherwise if the text is different or there is no text, #ifeq will display no.
example
This is useful in a template. For example the template:size, used to control the size of images on several pages.
template:size has the following on its page:
{{#ifeq:{{{1}}}|1| 20 | 40}}
If {{size|1}} then 20.
{{size}} alone displays 40.

Errectstapler 21:37, 13 May 2011 (UTC)

Originally it said "If .. Otherwise .." Now you say something only partly true ("The comparison is made as text"), then formulate an exception. The first seems better.--Patrick1 13:41, 15 May 2011 (UTC)
Hi Errectstapler, I second Patrick1's comment above, and find the addition of "text", "number", "parameter", "example" and "warnings" sub-headers visually unhelpful. I took the liberty of undoing your edits, but retained two useful things: the calls for the reader to notice the similar numerical examples, and the practical example. I hope I've managed to retain the spirit of your edits, while keeping the section congruent with the remaining sections of the page. Hamilton Abreu 02:06, 16 May 2011 (UTC)

Operators, again [edit]

It's taken me a long time to understand some of the operators listed in the table at the top-right of the page. In my opinion there could be much better documentation either inside the table or in the text of the article. I'm I retired "programmer" with a BS in computer science but most of my experience was in c, FORTRAN, and various database languages. I expected the div operator to perform integer division (as in c). I doesn't of course. I seems to be identical to the / operator It treats numbers as reals. Mod is a quasi integer operator because the output is an integer although the input can be a real. The comparison operators only work on numerical data. It would be nice some worked on alpha data. Of course #ifeq: can be used for equality. The warning about floating-point errors is hard to condone but at least its documented although the circumstances under which failure can be predicted are not well defined (it says it depends on the server?). You can get away with almost anything if its a well documented constant behavior. I'm far from throwing up my hands and walking away but I find the current state of the documentation unforgivable. I know that coders are notorious for the failing to document but, in my experience its essential to have a go to documentation person. On one job we had a women who could not code to save her life but she was a wizard at documentation and so was a valued employee (almost indispensable). There should be someone around here like that. People like that are worth their wait in gold. (Rant over and I feel much better.)droll [chat] 09:04, 5 July 2010 (UTC)

I guess clicking the links to Help:Calculation, the floating-point bug description, etc was just too much work for you?
131.216.14.1 17:37, 25 August 2010 (UTC)
Dickish responses like 131.216.14.1 is the part of the reason this article is so shitty. Errectstapler 20:14, 12 May 2011 (UTC)

Numbers and magic words [edit]

I face some problems when using #expr with some number formats like Arabic-Hindi (٠,١,٢,٣,٤,٥,٦,٧,٨,٩). This also arises when using some magic words such as CURRENTWEEK, which I believe produces numbers in the previous format. Is there a solution, or how can I change my default numbering format?--Email4mobile 17:05, 15 July 2010 (UTC)

Checking #tag for errors with #iferror doesn't work [edit]

I have a tag extension which returns an valid error in some cases. So I tried to use it with #iferror like {{#iferror: {{#tag:mytag |error producing input}} | ERROR |no error }} but it always returns no error. When I copy the output of {{#tag:mytag |error producing input}} into the #iferror function it works fine. I use MW 1.15 --Danwe 09:53, 25 July 2010 (UTC)

#expr [edit]

How can I get a decimal number produced here to a certain amount of decimal points?--Launchballer 20:09, 1 August 2010 (UTC)

With round.--Patrick 12:41, 2 August 2010 (UTC)
{{#expr:23.45678 round 2}} → 23.46
Told you the operators could use some documentation.  –droll [chat] 20:12, 2 August 2010 (UTC)

#ifeq Problem [edit]

When I try something like this:

{| border=1 colspan=0 colspacing=0 style="margin: 1em auto;text-align:center; font-size:90%;" 
|-
|In this box here.
{{#ifeq:{{FULLPAGENAME}}|Help talk:Extension:ParserFunctions||-
|Example.}}
|}

Then this is what shows up:

In this box here.

When it should read:

In this box here.
Example.

And the reasonwhy this is happening is because of the two pipe characters that are next to each other but are for different kinds of formatting but MediaWiki gets mixed up with the formatting and causes a problem. So is there a solution to this? --Ultra Dot 15:00, 30 August 2010 (UTC)

Yes. You need to escape the pipes that relate to the table, so that they are not interpreted by the parser. In most wikis, the template {{!}} exists for precisely this purpose. So:
{{#ifeq:{{FULLPAGENAME}}|Help talk:Extension:ParserFunctions|
{{!}}-
{{!}} Example.}}
should work. Hamilton Abreu 16:00, 30 August 2010 (UTC)
Hamilton Abreu beat me to it. Note that the "pipes" (template) should appear at the beginning of a row, as where you'd put them in a regular wikitable. -DePiep 16:44, 30 August 2010 (UTC)
Doesn't seem to work the same way on wikia wikis, so I am asking this question: Do wikia wikis use MediaWiki? (NOTE: I am trying to use it in a wikia wiki) --Ultra Dot 10:11, 31 August 2010 (UTC)
Yes, they do use MW. I guess Template:! doesn't exist there. --Danwe 11:00, 31 August 2010 (UTC)
They do use it. But when I tried doing the suggestion it started showing up on all pages even if the page title (string 1) is different from string 2. --Ultra Dot 13:25, 31 August 2010 (UTC)

Expensive Parser Function: What_if_logically_not_executed? [edit]

I put a question at a Manual_talk, but it seems to be out of the loop. Could someone take a look overthere? -DePiep 14:06, 4 September 2010 (UTC)

Why doesn't negative time work? [edit]

If I use the {{#time}} function and I write

{{#time:j F|8 June}}

the output becomes 8 June naturally. And if I write

{{#time:j F|8 June + 1 day}}

the output becomes 9 June. However if I write

{{#time:j F|8 June - 1 day}}

the output doesn't become 7 June as expected, but a big Error: Invalid time. Why on earth is that? On "my" Wikipedia (Swedish language version) it seems to work for some months (for instance, June works, but January doesn't) but on the English, it doesn't seem to work for any month at all. Could somebody shed som light on this please? /Ludde23 18:14, 13 September 2010 (UTC)

The help page states "The date/time object can be in any format accepted by PHP's strtotime() function." This link states that "The function expects to be given a string containing a US English date format [...]." So,
{{#time:j F|June 8}}8 June
{{#time:j F|June 8 + 1 day}}9 June
{{#time:j F|June 8 - 1 day}}7 June
seems to work. Could this be it? Hamilton Abreu 02:39, 15 September 2010 (UTC)

#time a day out with dates in certain months! [edit]

Hi.

If I feed 25 September 2010 in via

{{#time:l j F Y|{{{1|}}}}}

it returns Friday 24 September 2010. Inputting 4 October 2010 returns Sunday 3 October 2010. So both are one day out!

But if I feed 3 November 2010 in it returns Wednesday 3 November 2010, which is the correct day.

Any idea what is going on here? I'm running MW 1.15.3 and PF 1.1.1
Mitchelln 15:44, 27 September 2010 (UTC)

Any way to get the username? [edit]

Is there any way to detect the username of the person looking at a given page, so you could have the code respond "Greetings, <username>" for example? This is a basic programming exercise, so I was surprised that there doesn't seem to be any documented way to do it with Mediawiki, unless I just haven't been looking in the right place.

Search CURRENTUSER and find the variables extension. --Subfader 16:41, 27 September 2010 (UTC)
There's a variable "currentusers" but that generates a list of all users who are currently logged in, not the ones who are looking at a given page. On the English Wikipedia, there's a variable "currentuser" but that seems to return the last person who edited the page, not the person looking at it.

How to refer to / check on Semantic Properties? [edit]

I'm currently having problems to display a HintBox-Template based on the Semantic Property Values set for a page. How do i refer to the Property and do a comparison in a IF-Function.

Example: The Property: PageStatus
The possible Values: Draft, Review, Release

Result: Depending on the Property state a small hintbox should appear on top of my article. I'm not sure if i should use if, ifexpr or switch here (tendency going to "switch"). --Fennyface 09:12, 27 October 2010 (UTC)

Puzzled by different results on #ifeq related to whitespace and nowiki [edit]

I saw this example, and it doesn't make sense to me:

Original:

{{#ifeq: <nowiki />foo<nowiki /> | <nowiki />foo<nowiki /> | equal | not equal }}
not equal
{{#ifeq: <nowiki />foo<nowiki /> | <nowiki />foo<nowiki /> | equal | not equal }}
- not equal - Manually retyped to make sure the spaces were real


Isn't this what it looks like after the nowiki is removed?

{{#ifeq: foo | foo | equal | not equal }}
equal

Can someone explain why these #ifeq expressions above are not equivalent, and why the last one returns the expected result, and the first two don't return equal?

User:173.116.103.237, 17:19, 8 December 2010 (UTC)

I think the <nowiki/> tags are temporarily expanded during parsing, using unique identifiers, with the effect that they are each treated as non-identical. — Richardguk 19:54, 8 December 2010 (UTC)

Where is #tag documentation [edit]

I couldn't find #tag in the list with #if, #ifeq, etc. Where is it documented?

Help:Magic words#Miscellaneous.--Patrick 09:49, 19 December 2010 (UTC)

Declaring variables? [edit]

Is there a possibility to declare variables with the Parser Functions (or maybe an extension)? An example:

  • if {{{param}}} is "foo", {{{color}}} should be red and {{{width}}} 50
  • if {{{param}}} is "bar", {{{color}}} should be green and {{{width}}} 100
  • at the end of the code, there is one code for all cases, like this:

<div style="color: {{{color}}}; width: {{{width}}}px">Text</div>

At the moment, I have to write a code like this:
{{#switch: {{{param}}} |foo = <div style="color: red; width: 50px">Text</div> |bar = <div style="color: green; width: 100px">Text</div> }}
I hope you understand what I want to say.--Harmageddon 13:41, 20 February 2011 (UTC)

#vardefine is way easier: {{#ifeq: {{{param|}}} | foo | {{#vardefine:width|50}} {{#vardefine:color|green}} | (else) }}. Then use {{#var:width}} and {{#var:color}}. --Subfader 16:20, 21 February 2011 (UTC)
I think questions to the extension are better off in the support desk, so I transcluded this and replied there, please check: Project:Support Desk#Declaring_variables.3F_2580. --Theaitetos 21:15, 25 February 2011 (UTC)

Rounding to 1000 [edit]

I want to round to even thousand units, e.g. 12,345 should become 12,000 or 123,456.9 > 123,000. I thought something like this would work: "Over {{#expr:{{NUMBEROFARTICLES:R}}|round -3}} articles" (as here). --Subfader 13:06, 6 March 2011 (UTC)

Fix: {{#rreplace:{{NUMBEROFARTICLES}}|[0-9][0-9][0-9]$|000}} --Subfader 17:48, 6 March 2011 (UTC)

iftrue [edit]

Isn't there also a {{#iftrue: function? Mglovesfun 13:50, 2 May 2011 (UTC)

No, although there was a template by that name on the English Wikipedia several years ago. —Emufarmers(T|C) 22:14, 2 May 2011 (UTC)

Fully (recursively?) substituting within a switch [edit]

Hi, I'm trying to write a template that will randomly substitute one of four messages. I've got the random part down by using a mod function with time, but I can't get just the text to substitute. When I call it with a {{subst:randomtemplate}}, it grabs a new time and therefore a new message. It seems like a recursive template as in the docs, and I've tried every combination of <includeonly>subst:</includeonly> I can find anywhere in the template, and I still can't get it to work. I'd even settle for leaving the switch function in place and substituting the mod time value, but I can't even get that to work.

Template so far:

{{#switch: {{<includeonly>subst:</includeonly>{{#expr:{{#time: s}} mod 4}}}}
|0=message 1
|1=message 2
|2=message 3
|3=message 4
}}

Thanks! Staeiou 03:44, 11 July 2011 (UTC)

Figured it out, turns out you have to subst everything and put includeonly tags around the entire template. So behold, a random template generator:
<includeonly>{{subst:#switch: {{subst:#expr:{{subst:#time: s}} mod 4}}
|0=message0
|1=message1
|2=message2
|3=message3
</includeonly>
Staeiou 19:16, 11 July 2011 (UTC)

Trimming [edit]

I wish the parser functions did not trim the leading/trailing spaces and other characters. I am having trouble adding spaces and paragraph marks on these functions. Ganeshk 15:51, 24 September 2011 (UTC)

Using #if to generate conditional table rows [edit]

I'm running into a problem using #if to generate multiple conditional rows in succession. The issue, I believe, stems from line breaks, or lack thereof. If you use line breaks to separate the #if statements, new rows created with |- will appear correctly, but when the condition is false, the line breaks are still generated as white space (html P or BR), causing the last cell to have an extra line. On the other hand, if you keep the #if statements on the same line, there appears to be no way to generate a line break at the start of the output because the parser strips it. So |- will not appear on a new line, and therefore fails to generate a new row (it instead just gets rendered as text). 76.253.0.17 03:12, 26 September 2011 (UTC)

Same probleme here. Found any solution? Drhirn 13:03, 8 November 2011 (UTC)
Why not just just use html tags? --Subfader 18:14, 8 November 2011 (UTC)
Try using the same-line method and placing a invisible character (like &8201; or &#8203;) at the beginning of the output, before the line break. The innocuous character will then be added to the end of previous cell, and the line break will not be ignored. Note:&8201; is a very thin space and &#8203; is a zero-width space. Gustronico 17:47, 17 November 2011 (UTC)

#switch [edit]

How many parameters can I put into a single #switch?--90.217.236.107 09:59, 22 October 2011 (UTC)

Multiple #if [edit]

Is it possible to use the #if function for multiple articles? For example, IF this article AND that article exist, THEN 1; IF this article exists and that NOT, THEN 2; IF this article does NOT exist and that exists, THEN 3; IF both articles do NOT exist, THEN 4. I tried it many times using IF in IF, but it was effectless. Thanks.--Kiril Simeonovski 10:47, 27 December 2011 (UTC)

I would use {{#expr: {{#ifexist: <ARTICLE1> | 1 | 0 }} + {{#ifexist: <ARTICLE2> | 2 | 0 }} }}, which will give you values 3, 2, 1 and 0, respectively, for the four cases you described. Or if the numbers are particularly important, by playing around a bit you should be able to get the exact numbers you want out. Happymelon 12:16, 27 December 2011 (UTC)
Thanks once again. I didn't even imagine it's possible to use "+" sign.--Kiril Simeonovski 13:34, 27 December 2011 (UTC)

Checking if an external link exists [edit]

Is there a way to use #if, #ifeq or other ParserFunctions to check if an external URL exists or not (i.e., whether a web page is a dead link)? Smuconlaw 17:59, 15 January 2012 (UTC)

No. The wiki parser knows the status of its own pages only by checking its internal database (it does not actually try to load the webpage). It has no way of knowing whether pages exist if they are on unrelated domains. On some wikis, experienced editors occasionally run bots to check the status of external links and edit wikitext containing broken links, but that is unrelated to parser functions. — Richardguk 22:50, 15 January 2012 (UTC)
Ah, OK. Thanks. A template ({{Singapore Statute}}) at the English Wikipedia relies on an external website, but the managers of that website have launched an updated website which is quite different. I was wondering if the template could be tweaked to check if the old website is still accessible, and, if not, to access the new website instead. Looks like I should wait till the old website has been completely turned off, then rejig the template to link to the new website. — Cheers, SMUconlaw 09:56, 16 January 2012 (UTC)

ifeq match this or that [edit]

Is there a way to compare multiple values with a string?

{{#ifeq: this | value1 OR value2 OR value3 | yes | no }}

I know I could nest the #ifeq-tags but that results in ugly tag soups.

I could also use #switch but then i have to repeat the output for each. --Subfader (talk) 18:23, 26 March 2012 (UTC)

#switch will accept multiple possibilities for each output, so the following would output "yes" for this equal to any of "value1", "value2" or "value3", or output "no" otherwise:
{{#switch: this | value1 | value2 | value3 = yes | #default = no }}
See Help:Extension:ParserFunctions#Grouping results for a different example.
Richardguk (talk) 18:41, 26 March 2012 (UTC)
Oh, great :) --Subfader (talk) 06:49, 27 March 2012 (UTC)

No integer division ??? [edit]

Why is there no integer division for #expr ? There is a modulo operator but no integer division. This is not logical to have one feature and not the other.

We have to subtract 0.5 and then round to 0 digits. Instead div should be the integer division and / should be the decimal division.

--DavidL (talk) 13:32, 15 April 2012 (UTC)

Template:Ifnumber [edit]

To simplify a complex template, I made a Template:Ifnumber witch detect if an expression is numeric, but else do not generate any error.
example : {{Ifnumber| 231 | true text | false text }}
Is it a short equivalent, or this template could become a parse function ? Rical (talk) 15:29, 15 April 2012 (UTC)
I don't think we need to add another ParserFunction for a feature that can be so easily achieved with a short amount of code using existing functions. Happymelon 14:11, 17 April 2012 (UTC)

Creating a new line... [edit]

I am wondering if any of you guys can help me. I am attempting to use ParserFunctions to create a unit converter for my wiki. The issue is that the "#expr" code is creating a new line after every use. My code is below. Any help would be SOOOOO appreciated! Thanks!!! --Zackmann08 (talk) 22:22, 1 May 2012 (UTC)

The template:

<includeonly>"{{#expr:({{{1}}}* 2.2046)round2}} lbs ({{{1}}} kg)"</includeonly>

The use of the template:

* '''Weight''': {{convert/kg|1}}
* '''Weight''': {{convert/kg|2}}

What it produces:

  • Weight: "2.2 lbs (1 kg)"

  • Weight: "4.41 lbs (2 kg)"


What I WANT it to produce:

  • Weight: "2.2 lbs (1 kg)"
  • Weight: "4.41 lbs (2 kg)"

Hello, you have to make sure that the transcluded part is inline (when you need it to be). This means no Enters in the part that will be transcluded with the template. This can go wrong when you transclude a category with a template. If there is a Enter after the category it will get transcluded. Also, when you save the page starting with a Enter it will not show when you edit it again but it is still there. If you want more layout control we always exactly "tell" the wiki engine what needs to be transcluded. See example that will work below (on MW 1.17.2):
[[category:test]]<noinclude>
== Any Template documentation ==
* Bla bla 1
* Bla bla 2
* Bla bla 3</noinclude><includeonly>"{{#expr:({{{1}}}* 2.2046)round2}} lbs ({{{1}}} kg)"</includeonly>

When you have a Enter between the category and the <noinclude> it will mess up the result. --Jongfeli (talk) 11:46, 2 May 2012 (UTC)


THANK YOU SOOOOO MUCH!!!!! That really helped!!! --Zackmann08 (talk) 22:20, 2 May 2012 (UTC)

lenght of a string [edit]

Hi, I just want to share parser function to find lenght of a given string.You can find the template here, but it is in Turkish. It is up to 10 characters but you can increase. the size.

  • code =

{{ #ifeq: {{padleft:|1|{{{1|}}}}} | {{{1|}}} | 1 | {{ #ifeq: {{padleft:|2|{{{1|}}}}} | {{{1|}}} | 2 | {{ #ifeq: {{padleft:|3|{{{1|}}}}} | {{{1|}}} | 3 | {{ #ifeq: {{padleft:|4|{{{1|}}}}} | {{{1|}}} | 4 | {{ #ifeq: {{padleft:|5|{{{1|}}}}} | {{{1|}}} | 5 | {{ #ifeq: {{padleft:|6|{{{1|}}}}} | {{{1|}}} | 6 | {{ #ifeq: {{padleft:|7|{{{1|}}}}} | {{{1|}}} | 7 | {{ #ifeq: {{padleft:|8|{{{1|}}}}} | {{{1|}}} | 8 | {{ #ifeq: {{padleft:|9|{{{1|}}}}} | {{{1|}}} | 9 | {{ #ifeq: {{padleft:|10|{{{1|}}}}} | {{{1|}}} | 10 | 11}}}}}}}}}}}}}}}}}}}} Saltinbas (talk) 15:15, 14 May 2012 (UTC)

#switch/#ifeq with apostrophes [edit]

For example, if you're matching a pagename that contains an apostrophe to the plain text version

{{#ifeq: {{PAGENAME}}|The Page's Name|y|n}}

the result always comes out to be 'n', oddly.

Anyone know a loop around this? --108.224.95.15 05:50, 25 July 2012 (UTC)

Nevermind, replacing the apostrophe with &#39 ; works. --108.224.95.15 05:54, 25 July 2012 (UTC)

replacing #ifexist with #dpl [edit]

The behaviour of #ifexist can be reproduced with DPL using the following syntax:

{{#dpl:title= page title |oneresultheader= value if exists |noresultsheader= value if doesn't exist }}

which is equivalent to:

{{#ifexist: page title | value if exists | value if doesn't exist }}

DPL has the advantage that it does not create a link between the target page (page title) and the page containing the query. Using #ifexist would cause your page to be listed in Special:WhatLinksHere/page_title.

Capmo (talk) 06:42, 18 August 2012 (UTC)

#ifexist is faster. Avoid calling DPL wherever you can. --Subfader (talk) 13:23, 19 August 2012 (UTC)
DPL makes direct queries to the database, what can be faster than this? And there must be a reason for #ifexist to be labeled as an "expensive parser function". The wiki I administer uses DPL extensively for all sorts of things, and we don't feel any perceivable degradation in server performance. Capmo (talk) 21:53, 21 August 2012 (UTC)
Initializing DPL on page load takes longer than ParserFunctions.
"#ifexist to be labeled as an "expensive parser function"" that's because the DB load gets expensive on calling it X times, but the same counts for calling DPL X times on a page... --Subfader (talk) 11:50, 22 August 2012 (UTC)

If A or B or C (resolved) [edit]

I am trying to use the if statement to test for multiple different values.

For example: {{#if: {{{a|}}}{{{b|}}}{{{c|}}}| YES | NO}} would display "YES" if any of the three (a,b,c) variables were defined. If, and only if, all 3 were NOT defined, it would display "NO".

I simply cannot get this to work! Any help would be most appreciated.

--Zackmann08 (talk) 01:09, 10 September 2012 (UTC)

Try it like this {{#if:{{{a|}}}|YES|{{#if:{{{b|}}}|YES|{{#if:{{{c|}}}|YES|NO}}}}}}. If one of the parameters has a value it will display YES, only when non of the parameters has a value it will display NO. In this way you can make any combination you want and make it as complicated as you want. --Jongfeli (talk) 09:03, 10 September 2012 (UTC)
The issue is that I am attempting to use this to do something much more complex than just displaying "YES" and "NO". I am using it to create a rather large table. Is there no way to do "If A or B or C"? --Zackmann08 (talk) 13:50, 10 September 2012 (UTC)
If I understand you correctly you would like to fill a (calculated) column in a table and the value depends on other values in the same row? I tried this in combination with Extension:External Data but {{#if: does not work because it probably always gets a value back from the Query. It does not matter if there is a database NULL in the table or an empty string. Just to see if it can work I tried {{#ifeq: (see example below) but it only works on the first row it displays, if in the first row a = 1 it will display Yes and it will repeat the result on the rest of the rows even if a is not 1.
  {{#for_external_table:expression}}
  {| class = "wikitable" 
  ! Column 1
  ! Column 2
  ! Column 3
  ! Test{{#for_external_table:<nowiki/>
  {{!}}-
  !{{{a}}}
  {{!}}{{{b}}}
  {{!}}{{{c}}}
  {{!}}{{#ifeq:{{{a}}}|1|Yes|No}}
  }}
Maybe I am doing something wrong but it seems that in combination with External Data the ParserFunctions does not handle each row individually. --Jongfeli (talk) 15:19, 10 September 2012 (UTC)
This is part of an infobox. I am trying to copy the technique used on wikipedia (For example: http://en.wikipedia.org/w/index.php?title=Template:Infobox_weapon&action=edit)
In the example below, the heading for the "Service History" only displays if "service", "used_by" or "wars" is defined. For some reason, this exact syntax is not working for me.
<!------------------------------Service history------------------------------>
{{#if:{{{service|}}}{{{used_by|}}}{{{wars|}}} |
! colspan="2" {{WPMILHIST Infobox style|header_bar}} {{!}} Service history
}}
|-
{{#if:{{{service|}}} |
! style="padding-right: 1em;" {{!}} In service
{{!}} {{{service|}}}
}}
|-
{{#if:{{{used_by|}}} |
! style="padding-right: 1em;" {{!}} Used by
{{!}} {{{used_by|}}}
}}
|-
{{#if:{{{wars|}}} |
! style="padding-right: 1em;" {{!}} Wars
{{!}} {{{wars|}}}
}}
|-
--Zackmann08 (talk) 15:47, 10 September 2012 (UTC)
So its now working. Not sure what i was doing wrong... Oh well! --Zackmann08 (talk) 16:26, 10 September 2012 (UTC)

If number check (resolved) [edit]

I am attempting to write an error checking function that checks to see whether a user has supplied either a year (####) or the word "Present". (This is for an infobox for a vehicle. I want to display the production years.) I am 99% of the way there with the following:

{{#iferror:{{#expr: 0 + {{{1}}}}}|{{#ifeq:{{{1}}}|Present|<!--DO NOTHING-->|<!--Display some error message-->}}}}

The issue that I am having is that if a valid date is supplied, then the expression ({{#expr: 0 + {{{1}}}}}) evaluates and is displayed. Is there an easy way to suppress that expression? I seem to remember seeing a "span display=none" type thing somewhere...

Any advice will be greatly appreciated.

--Zackmann08 (talk) 14:35, 19 September 2012 (UTC)

I love solving my own problems. :-) <span Style="display: None"> was my solution. --Zackmann08 (talk) 14:40, 19 September 2012 (UTC)
I also had a similar question because the parser functions give script errors in some cases. I had do this. --Rical (talk) 22:50, 19 September 2012 (UTC)

How to use tag function within parser function? [edit]

I'm trying to build an if-clause that automatically inserts

== References == <references />

into the article if there are some <ref>s entered in the wikitext. If there's no <ref> no heading and no references/ should be inserted.

I tried different things but none of them work - that means I always get "Heading and stuff" even if there's no ref tag entered in the article:

{{#if: <references /> | Heading and stuff | }}
{{#if: {{#tag:references|}} | Heading and stuff | }}

The docu says "Content inside parser tags (such as nowiki) is temporarily replaced by a unique code" and shows that tags won't work.
Is there a workaround to let <references /> be parsed inside the #if?
--Stefahn (talk) 17:38, 9 November 2012 (UTC)

Just for the record: Found a solution/workaround now :) Stefahn (talk) 17:59, 3 April 2013 (UTC)

#time: and Daylight Savings [edit]

Is there any flag for DST? Is there any intent to add a flag for this in the future if it currently doesn't exist? Thank you. -- ShoeMaker   ( Contributions Message )   18:30, 9 November 2012 (UTC)

Square root and other questions [edit]

Hello,
I had 2 questions about the {{#expr:}} and the {{#ifexpr:}} parser functions:

  1. Is it possible to add a square root to those functions? using the sign √ in a formula doesnt work to me.
  2. Also, what do the != and the <> logic keys do?

80.230.37.167 14:38, 26 November 2012 (UTC)

Oh, i see that != make negative solutions possitive, but i still need the square root thing.
80.230.37.167 14:49, 26 November 2012 (UTC)
 ! in front of any conditional punctuation means NOT, so != is NOT EQUAL. As far as your question about sqr roots, you could build a (set of) template(s) like http://meta.wikimedia.org/wiki/Template:Root -- 141.114.97.70 15:08, 26 November 2012 (UTC)
tHANK YOU 80.230.37.167 15:59, 26 November 2012 (UTC)

#time and full month names [edit]

Hi, I am apparently not seeing any indication of what any underlying support files are called.
The F operator returns a full month name, so 12 produces "December".
Which file houses the actual "December" string?
At my home wiki, it is returning the wrong string.
Thanks, Varlaam (talk) 05:41, 30 November 2012 (UTC)

On "my" wiki, I believe it uses MediaWiki:December and/or MediaWiki:December-gen, but I'm not entirely sure. If you would like to link your an example on your home wiki so I can see what it is returning, I would be happy to take a look and try to help you fix it. -- ShoeMaker   ( Contributions Message )   22:42, 2 January 2013 (UTC)

Test for existence of Month or Day in Date string? [edit]

Is there some way to test if a day or month was part of a date string?

For example, {{#time r|2003}} only contains a year, but returns a string formatted with the current month and day. I'd like to be able to return empty if passed on month or day (e.g. like {{#time n|2003}}, but returning empty if there is no month part of the date). Curly Turkey (talk) 23:57, 31 December 2012 (UTC)

I want to help you, but I'm not entirely sure what you are asking for.
{{#time:r|2003}} returns Sat, 24 May 2003 00:00:00 +0000
{{#time:n|2003}} returns 5
I'm assuming you are hoping to use this as part of a template that will have values passed in to it, is that correct? If so, what is your list of arguments, what do you hope the template can do, and do you have a link to the template creation sandbox or wherever that you have started to make this so I can take a peek and hopefully understand what you want. -- ShoeMaker   ( Contributions Message )   22:52, 2 January 2013 (UTC)

Expression error: Unexpected < operator. [edit]

I'm trying to add Extension:DPL Page Name S/N Maker (using SemanticForms) (which is really just a complex template combining multiple other extensions) to a corporate wiki, but for any result higher than "00001" it keeps kicking out the error message Expression error: Unexpected < operator. This error isn't covered on the help page here, but based on the documentation I assumed that it must be a stray < character or possibly a tag inside a function where tags aren't allowed. However, even when I remove all instances of the < character from the template (by eliminating the comments, the <strong> tags, and the <noinclude> tags), the error message persists.

Here is the text of the template:

[removed]

Any idea what the problem is and/or how I can correct it? Thanks! ~ Michael Chidester (Contact) 15:07, 22 March 2013 (UTC)

Solution [edit]

Ultimately I determined that this was a DPL conflict, possibly due to the {{expr:}} function refusing to parse the standard output format of DPL. To help anyone having this problem in the future, here is my modified code, changing out the deprecated string functions for their replacements and modifying the DPL output to produce a regular string rather than a link:

{{#if: {{{form|}}}
  |  {{#ifexist: Form:{{{form|}}} 
       |
       | <strong class="error">Warning! The form "{{{form|}}}" may not be a valid form name!</strong>
     }}
  |
}}<!---
-->{{#if: {{{form|}}}
     | {{#ifeq: {{lc:{{NS:{{{namespace|}}} }} }} 
         | {{lc:{{{namespace|}}} }}
         | 
         | <strong class="error">Warning! "{{{namespace}}}" may not be a valid namespace name!</strong>
       }}
     |
   }}<!---
-->The next document number is '''<!--
-->{{#if: {{{namespace|}}} | {{{namespace|}}}: }}{{{prefix|}}}<!--
   -->{{padleft:<!----------- (X+1)                         =>  0000(X+1) 
      -->{{#expr:<!---------- 0000X+1                       =>  (X+1)
         -->{{replace|<!----- "Namespace:prefix0000X"       =>  "0000X" 
            -->{{replace|<!-- "*[[Namespace:prefix0000X]]"  =>  "Namespace:prefix0000X"
               -->{{#dpl:<!-- return pages named "*[[Namespace:prefix0000X]]"
                  -->titleregexp =^{{{prefix|}}}[0-9]{{{{pad|5}}}}$<!--
                  -->| namespace = {{{namespace|}}}<!--
                  -->| order = descending<!--
                  -->| mode = userformat<!--
                  -->| format = ,%PAGE%<!--
                  -->| noresultsheader = {{padleft:0|{{{pad|5}}}|0}}<!--
                  -->| count = 1
                  }}
               |/^([^{{!}}]+\{{!}}){1}([^\]]+).*/  
               |\2
               }}
            | {{#if: {{{namespace|}}} | {{{namespace|}}}:}}{{{prefix|}}}
            |
            }}
         +1
         }}
     | {{{pad|5}}}
     | 0
     }}'''
----
{{#if: {{{form|}}}
  | {{#forminput: {{{form|}}}
      | {{{width|20}}}
      | {{{prefix|}}}<!--
      -->{{padleft:<!----------- (X+1)                         =>  0000(X+1) 
         -->{{#expr:<!---------- 0000X+1                       =>  (X+1)
            -->{{replace|<!----- "Namespace:prefix0000X"       =>  "0000X" 
               -->{{replace|<!-- "*[[Namespace:prefix0000X]]"  =>  "Namespace:prefix0000X"
                  -->{{#dpl:<!-- return pages named "*[[Namespace:prefix0000X]]"
                     -->titleregexp =^{{{prefix|}}}[0-9]{{{{pad|5}}}}$<!--
                     -->| namespace = {{{namespace|}}}<!--
                     -->| order = descending<!--
                     -->| mode = userformat<!--
                     -->| format = ,%PAGE%<!--
                     -->| noresultsheader = {{padleft:0|{{{pad|5}}}|0}}<!--
                     -->| count = 1
                     }}
                  |/^([^{{!}}]+\{{!}}){1}([^\]]+).*/  
                  |\2
                  }}
               |{{#if: {{{namespace|}}} | {{{namespace|}}}:}}{{{prefix|}}}
               |
               }}
            +1
            }}
        | {{{pad|5}}}
        | 0
        }}
     | {{{buttonlabel|Add}}}
     | {{#if: {{{namespace|}}} | namespace = {{{namespace|}}} }}  
     }}
  | <includeonly><strong class="error">Warning! You haven't specified a SemanticForm for page creation! Please specify a SemanticForm name.</strong></includeonly>
}}<noinclude>
{{documentation}}
</noinclude>

Cheers. ~ Michael Chidester (Contact) 15:40, 26 March 2013 (UTC)

See also Scribunto ? [edit]

Now, we have modules in scribunto/Lua and the syntax to call them is " {{#invoke| ". Then users will try to search them here and we could add Extension:Scribunto to "See also" paragraph ? --Rical (talk) 18:13, 11 May 2013 (UTC)

I think you should create a section Extension:Scribunto/Lua reference manual#invoke on the extension page, since that should bring this section to the top of the search results when people search "invoke". --Theaitetos (talk) 22:11, 12 May 2013 (UTC)