Template talk:If

From Wikipedia, the free encyclopedia

Contents

[edit] Purpose

if provides if..then..else functionality for use in complicated templates.

Several widely used templates such as {{web reference}} (doc/talk) and {{book reference}} (doc/talk) used to rely on if. They have been switched over to the server friendlier and faster {{qif}} (doc/talk).

[edit] Usage

[edit] Old style variant

Old-style usage. No longer recommended due to possible equal sign problems (see m:Help:Template#Equals sign in parameter value).

{{if

 |VALUE-TO-BE-TESTED
 |text to show if not blank (may include VALUE-TO-BE-TESTED)
 |text to show if param is blank

}}

[edit] Named parameters variant

Alternate usage. Prevents equal sign problems (see m:Help:Template#Equals sign in parameter value). Can be easily switched to the server friendlier template:qif (talk) by just replacing the name of the called template from if to qif.

{{if

 |test=VALUE-TO-BE-TESTED
 |then=text to show if not blank (may include VALUE-TO-BE-TESTED)
 |else=text to show if param is blank

}}

[edit] Examples

if has been used in {{language}} (doc/talk), {{web reference}} (doc/talk) and {{book reference}} (doc/talk).

They all have been converted to the server friendlier and faster {{qif}} (doc/talk).

[edit] Implementation

if works by calling {{show1}} (doc/talk), passing to show1 a parameter name as well as a parameter value based on the values of parameters passed to if. This depends on the specific technical feature that "(..) multiple assignments of values to the same parameter in the same template call are allowed and result in the last value being used." (see "A parameter name depending on a parameter" in meta:Help).

if takes three parameters (ignoring alternate usage for simplicity):

  • the "test" value {{{1}}} [required]
  • the "do-if-test-not-empty" value {{{2}}} [required]
  • and the "do-if-test-is-empty" value {{{3}}} [optional]

if contains a call to show1 with two parameter assignments. This is implemented as (ignoring alternate usage):

{{show1|1={{{2}}}|1{{{1}}}={{{3}}}}}

show1 simply echoes the value of its parameter 1. The call's first parameter assignment 1={{{2}}} sets unnamed parameter 1 of show1 to the "do-if-test-not-empty" value.

Then there is a second parameter assignment: 1{{{1}}}={{{3}}}. The name of the parameter in this assignment is "1" plus the test value. If the test value is empty, the name of the parameter is just "1", and this assignment overrides the first assignment, setting parameter "1" of show1 to the "do-if-test-is-empty" value.

For example, {{if|a|b|c}} is expanded as {{show1|1=b|1a=c}} which shows parameter 1 which is b, the second 1a=c statement being ignored, since 1a is not the name of parameter 1.

{{if|b|c}} is expanded as {{show1|1=b|1=c}} which shows parameter 1 which is c. The first assignment 1=b sets parameter 1 of show1 to b, but the second assignment overrides this by setting parameter 1 to c.

[edit] See also

[edit] Tests

# Code Expected
Result
Result of
Template:if
1
{{if
  |test=foo
  |then=true
  |else=false
}}
true true
2
{{if
  |test=
  |then=true
  |else=false
}}
false false
3
{{if
  |test=[]
  |then=true
  |else=false
}}
true true
4
{{if
  |then=true
  |else=false
}}
false false
5
{{if
  |test=()
  |then=true
  |else=false
}}
true true
6
{{if
  |()
  |true
  |false
}}
true true
7
{{if
  |
  |true
  |false
}}
false false
8
{{if
  |2=true
  |3=false
}}
false false
9
{{if
  |test={{{1|}}}
  |then=true
  |else=false
}}
false false
10
{{if
  |test=""
  |then=true
  |else=false
}}
true true
11
{{if
  |test=""
  |then=true
}}
true true
12
{{if
  |test=
  |then=true
}}
13
{{if
  |test=
  |else=false
}}
false false
14
{{if
  |test=foo
  |then=true
}}
true true

[edit] History

Reformated to "standard" 
AzaToth 00:53, 26 November 2005 (UTC)

[edit] Discussion

[edit] Language and Category link

I have has to move the innter wiki language and category link out of the template space and onto the the talk page. Even though it does not render the txt in the article affected it creats a break space that broke at least one of the templeates that i used the if on, Template:GAA county infobox, perople seriouls need to think of that tthey are doing before they make changes to templates and need to check to make sure those changes do not mess up the work of other people. --Boothy443 | comhrá 05:55, 22 October 2005 (UTC)

Apparently, someone put the category tag back in at some point. I've removed the category tag. // Pathoschild 03:57, 4 November 2005 (UTC)

[edit] Suggestion for update

{{show1
  |1={{{2|{{{true|{{{then|}}}}}}}}}
  |1{{{1|{{{expr|{{{test|}}}}}}}}}={{{3|{{{false|{{{else|}}}}}}}}
}}

So this will work:

{{if
  |test=foo
  |then=yes
  |else=no
}}

and/or

{{if
  |expr=foo
  |true=yes
  |false=no
}}

--AzaToth talk 13:54, 24 November 2005 (UTC)

I would like to propose to remove the variant (expr,true,false). (test,then,else) is better and sufficient. – Adrian | Talk 20:39, 24 November 2005 (UTC)
Variant (test,then,else) is used in {{web reference}} now (see revision as of 00:41, 25 November 2005 by me). – Adrian | Talk 00:49, 25 November 2005 (UTC)

[edit] Syntax

Perhaps use the same syntax as for Template talk:switch? AzaToth 00:16, 26 November 2005 (UTC)

May I ask to explain a bit? Motivation? How to implement? Impact on existing uses of if? – Adrian | Talk 19:36, 30 November 2005 (UTC)
Nah, it was just how the talk page should look like, it irrelevant now AzaToth 19:43, 30 November 2005 (UTC)

[edit] New if

Current code proposal
{{{else{{{test|{{{1|}}}}}}|{{{3{{{test|{{{1|}}}}}}|{{{test{{{test|}}}|{{{1{{{1|}}}|{{{then{{{then{{{test|{{{1|}}}}}}|}}}|{{{2{{{2{{{test|{{{1|}}}}}}|}}}|}}}}}}}}}}}}}}}}}}
Link
User:AzaToth/X1
Testcases
User talk:AzaToth/X1

[edit] Discussion

This could work, can anyone confirm? {{{else{{{test|{{{1|}}}}}}|{{{3{{{test|{{{1|}}}}}}|{{{then|{{{2|}}}}}}}}}}}}

:I'm just reporting that this nice proposal was made by User:AzaToth at 13:02, 26 November 2005 (UTC). He just forgot to sign his post. – Adrian | Talk 18:59, 26 November 2005 (UTC)

Wooha. Another application of Brainfuck :-). Please hold the line (computing at 0.000046 "|"-chars per second...). – Adrian | Talk 15:58, 26 November 2005 (UTC)
This syntax might create the same problems as the similar version of {{boolor}}. Mediawiki parses this kind of statement in an unusual way. If you want test space, you can use {{X1}}, {{X2}} etc. and a personal sandbox. --Gareth Hughes 16:15, 26 November 2005 (UTC)
Thanks. I do have my own x1 and x2 under my user space (User talk:Adrian_Buehlmann/x2). I do tests on the associated talk pages. I don't like {{X1}} et al. any more. Another example for a user test is User talk:Adrian_Buehlmann/book_coauthors (left over from a userfied TfD). – Adrian | Talk 16:46, 26 November 2005 (UTC)
My analysis:

{{{else{{{test|{{{1|}}}}}}|{{{3{{{test|{{{1|}}}}}}|{{{then|{{{2|}}}}}}}}}}}}

      [-------A---------]     [-------B---------] [--------C--------]
                          [----------D-----------|----------E-----------]

[----------F--------------|--------------------G---------------------------]

Assume we have {{if|test=X|then=Y|else=Z}} then we can simplify this to:

{{{else{{{test}}}|{{{3{{{test}}}|{{{then}}}}}}}}}

      [---A----]     [---B----] [---C-----]
                 [-----D-------|-----E-------]

[------F---------|-------------G----------------]

{{if|test=X|then=Y|else=Z}} is expanded as:
*Assuming X is non-empty (case (a)):
{{{elseX|{{{3X|Y}}}}}} variable 3X of if is not defined, so this is expanded as:
{{{elseX|Y}}} variable elseX of if is not defined, so this is expanded as:
Y
*Assuming X is empty (case (b)):
{{{elseX|{{{3X|Y}}}}}} is expanded as
{{{else|{{{3|Y}}}}}} parameter else of if is defined, so {{{3|Y}}} is ignored, so this is expanded as:
Z
So, at first sight this could work as expected (if mediawiki works how I thought). Another question is, what is/are the benefit(s) of this new solution compared to the existing one? – Adrian | Talk 17:26, 26 November 2005 (UTC)
There are no external meta calls. AzaToth 17:29, 26 November 2005 (UTC)
On an other note, if we should force people to user either 1,2,3 or test,then,else, the code reduces to:
{{{else{{{test|}}}|{{{then|}}}}}}
or
{{{3{{{1|}}}|{{{2|}}}}}}
The current syntax I wrote so you could mix numerical and textual parameters. AzaToth 17:47, 26 November 2005 (UTC)
On a side note, during the time you made your analysis, you computed 0.242880 "|"-chars (((17*60+26)*60-(15*60+58)*60)*0.000046) Template:;-) AzaToth 17:59, 26 November 2005 (UTC)
Carl, you've convinced me. Thanks for presenting your idea here. Se here is my stamp: APPROVED. – Adrian | Talk 18:46, 26 November 2005 (UTC)
  • Sadly, I forgot one case, thats when 3 or then is undefined, it can be fixed I suppose, but the code will look more ugly :-), one I made look like this {{{then{{{else{{{test|{{{1|}}}}}}|{{{then{{{test|{{{1|}}}}}}|{{{3{{{test|{{{1|}}}}}}| {{{2{{{test|{{{1|}}}}}}|}}}}}}}}}}}}|{{{2{{{else{{{test|{{{1|}}}}}}|{{{then{{{test|{{{1|}}}}}}| {{{3{{{test|{{{1|}}}}}}|{{{2{{{test|{{{1|}}}}}}|}}}}}}}}}}}}|{{{else|}}}}}}}}}
  • Perhaps it's better to just do a new if (Template:qif or simlar) that only takes test,then,else and all parameters must be defined {{{else{{{test|}}}|{{{then|}}}}}}
  • {{qif|test=foo|then=bar|else=biz}} AzaToth 20:36, 26 November 2005 (UTC)
Never mind. I didn't see that either (although I'm not a template specialist :-). Anyway it's always a good thing to present ideas first, even if it helps oneself to find an error. So I hope your're not frustrated about that now and continue to have ideas! At the moment, I would keep our good old friend if as it is. Now we know its value even better. In re qif: I think that would be hard to "sell" to the community because if, as it is, works. So, there is no need to change or add anything. – Adrian | Talk 21:14, 26 November 2005 (UTC)
  • I think I fixed it, I added a check to try to return the value of (test plus the value of test) before I return the value of then: {{{else{{{test|{{{1|}}}}}}|{{{3{{{test|{{{1|}}}}}}|{{{test{{{test|}}}|{{{1{{{1|}}}|{{{then|{{{2|}}}}}}}}}}}}}}}}}}, see test cases below. AzaToth 22:00, 26 November 2005 (UTC), edited by – Adrian | Talk 00:26, 27 November 2005 (UTC)

[edit] Logic templates on WP:AUM

See the discussion on Wikipedia_talk:Avoid_using_meta-templates#Logic_templates. – Adrian | Talk 15:52, 9 December 2005 (UTC)

[edit] Deprecation

Effects discussed with the devs on #wikimedia-tech earlier today. "Kill it with a stick." - David Gerard 19:44, 18 December 2005 (UTC)

Thanks. I was just in the process of converting calls of if to qif. Think I have done already a bunch of them so what you've done won't hurt that much. So why don't you do just the REAL thing and kill qif now? I would love to watch that! At least I do not need working on converting to qif any longer. Thanks! – Adrian | Talk 20:03, 18 December 2005 (UTC)
As our DBA, Jamesday put it, don't proudly crow about possibilities of denial of service. As he also put it, don't be a dick - David Gerard 21:15, 18 December 2005 (UTC)
Put on TfD. Not used. – Adrian | Talk 09:22, 23 December 2005 (UTC)

[edit] Status

The WP:AUM 'server load' concerns above were eventually stated to be unfounded by the lead developer, but this template and others are now deprecated in favor of m:ParserFunctions which perform the same tasks. --CBD 15:04, 19 June 2006 (UTC)