Wikipedia talk:Conditional tables

From Wikipedia, the free encyclopedia

Contents

[edit] MUST use HTML

  • Actually, you MUST use HTML to hide rows with Qif, because otherwise the pipes of the table syntax conflicts with those of the template syntax.
  • You'll want to reference the Boolean templates too

Circeus 14:07, 17 March 2006 (UTC)

For some reason I thought it might work with the table syntax if you used sub templates, like this–
Template:Foo
{| class="infobox"
{{qif|test={{{var|}}}|then={{Foo/Bar|{{{var|}}}}}}}
|-
! Baz
| {{{var2}}}
|}
Template:Foo/Bar
|-
! Foo
| {{{var}}}
At least that was an exception that I thought worked. =) Regarding boolean templates, I have very little experience with those. If you think you can write up a basic beginners guide, be my guest. =) If not, I'll try and add that as I learn about them. —Locke Coletc 16:14, 17 March 2006 (UTC)
I tried to understand Template:Boolexists (edit talk links history) because I don't like {{Qif}} and want a simpler {{ifdef|test|do}}, but so far that failed. Some test results on Template talk:Boolexists, note that these tests have nothing to do with the purpose of this template, i.e. test if a page exists. Omniplex 14:08, 19 March 2006 (UTC)

[edit] New attempt

Mixing XHTML with Wiki table syntax is a layer violation, and some uses in Category:Wikipedia header templates (edit talk links history) for the shortcut are incorrect, missing <tr> and similar issues, in other words FUBAR. But it should be possible to get it to work somehow with Wiki syntax.

{| class="infobox"
{{qif|test={{{foo|}}}|then=
{{!}}-
! Foo
{{!}} {{{foo}}}
}}
|-
! Bar
| {{{bar}}}
|}


Apparently that works, we need a shorter name for the template returning a vertical bar... ;-) Omniplex 13:57, 19 March 2006 (UTC)

Hm, we could always use {{TR}} and {{TD}}. Then the code turns into–
{| class="infobox"
{{qif|test={{{foo|}}}|then=
{{TR}}
! Foo
{{TD}} {{{foo}}}
}}
|-
! Bar
| {{{bar}}}
|}
Which ain't pretty, but might be better than pure (X)HTML. Regarding the header templates, I only looked at one I admit, but it looked like it was just defining another column in the already existing row (so no need for <tr> ... </tr>). The one thing I saw that did kind of bug me was the nesting of <th> within <td>. —Locke Coletc 21:01, 19 March 2006 (UTC)
Yes, at the time when you inserted it I had no clue what it's supposed to do, i.e. avoid the reserved "|" within Qif. A dummy template with output "|" might be cleaner. We don't need <th>, "!" is okay. Maybe a second dummy template for "||", but actually I'd say one is bad enough, and "||" is never required. Omniplex 21:55, 19 March 2006 (UTC)
I've trimmed my old comment with <s> reflecting Locke's comment and replaced {{tl2}} by {{tlp}} in the sample table. Omniplex 10:01, 21 March 2006 (UTC)
Well, if you just want to make a template for the vertical bar, I note that {{B}} and {{V}} are available. (B for bar, V for vertical-bar?). I know AzaToth prefers (and I somewhat agree) using straight (X)HTML for table syntax (in other words, no wiki-markup at all). I don't know that using a sub-template to get the bar would be any better or worse from a code-readability point of view though... I'm leaning towards worse, but I need to look at it more. HTML table syntax isn't that complicated though (really just five tags that people would need to know; TABLE, TR, TD, TH, and CAPTION). And the mix of HTML and {{qif}} can be neatly separated.
By all means though you should edit this in as another alternative if you think it'd be more readable. =) —Locke Coletc 22:10, 19 March 2006 (UTC)
Such a solution risk making styling these cells hell. What's so bad with XHTML in there?. Circeus 22:07, 20 March 2006 (UTC)
If what you need is say | style="color: red" | something red
you could use {{!}} style="color: red" {{!}} something red
As straight forward as possible. Omniplex 22:31, 20 March 2006 (UTC)
You said AzaToth prefers (and I somewhat agree) using straight (X)HTML for table syntax (in other words, no wiki-markup at all). While I disagree strongly in the case of layer violations (either use Wiki or XHTML table syntax, but don't mix them) XHTML syntax and exclamation mark are no issues wrt {{Qif}}, only the vertical bar has to be escaped from the parameter default mechanism. Normally &#124; is good enough, but within a table it needs a template. I've created Template:! (edit talk links history) for this purpose. Omniplex 22:20, 20 March 2006 (UTC)

[edit] Ifdef

Not really necessary to invent a third method, but I'm interested to get some ideas about its fragility:

{| class="infobox" {{ifdef|{{{foo|}}}|
{{!}}-
! Foo
{{!}} {{{foo}}}}}
|-
! Bar
| {{{bar}}}
|}

The code above is in {{qif_example_2/b}}. The table below demonstrates the effect when it's used:

Template call Result
{{qif_example_2/b}}
Bar {{{bar}}}
{{qif_example_2/b|foo=}}
Bar {{{bar}}}
{{qif_example_2/b|foo=|bar=vbar}}
Bar vbar
{{qif_example_2/b|foo=value}}
Bar {{{bar}}}
{{qif_example_2/b|foo=value|bar=vbar}}
Bar vbar

So far apparently fine, but it didn't work with at least one of the Wikipedia header templates.

The Template:Ifdef (edit talk links history) code is straight from Meta. It's the {{Qif}} version without else replacing the remaining named parameters test and then by positional parameters 1 and 2 resp.

Please don't try to fix it, I want it as simple as is, but it makes me nervous that I don't understand why it's so fragile. -- Omniplex 16:17, 27 March 2006 (UTC)

Answered by Paddu in Ifdef, executive summary: Use {{ifdef|test|2=then}} if then contains any "=". Minimalism has its drawbacks. -- Omniplex 22:15, 17 April 2006 (UTC)

[edit] TfD nomination of Template:Ifdef

Template:Ifdef has been nominated for deletion. You are invited to comment on the discussion at the template's entry on the Templates for Deletion page. Thank you. -- Omniplex 15:15, 18 April 2006 (UTC)

[edit] Direct ifdef approach

After some drawbacks with #if: and Ifdef have been identified (and for the latter documented) it's time to check out the m:Help:Parameter default mechanism directly with no if-construct at all as in the upgraded {{Shortcut|[[WP:QIF]]}}:

{| class="infobox" {{{foo{{{foo|}}}|
{{!}}-
! Foo
{{!}} {{{foo}}}}}}
|-
! Bar
| {{{bar}}}
|}

The code above is in {{qif_example_2b}}. The table below demonstrates the effect when it's used:

Template call Result
{{qif_example_2b}}
Foo {{{foo}}}
Bar {{{bar}}}
{{qif_example_2b|foo=}}
Bar {{{bar}}}
{{qif_example_2b|foo=|bar=vbar}}
Bar vbar
{{qif_example_2b|foo=value}}
Foo value
Bar {{{bar}}}
{{qif_example_2b|foo=value|bar=vbar}}
Foo value
Bar vbar

An expected minor difference in the first line, otherwise it works. This method is now documented in Advanced_templates on Meta.

There are apparently some drawbacks with substituting templates containing many ifdef-constructs, it replaces pointless code for ifndef cases. -- Omniplex 01:58, 22 April 2006 (UTC)

Putting the basic logic behind 'qif' and 'switch' directly into the final template like this has been called 'Weeble code' or the 'blank parameter trick' in the past. Examples previously in widespread use can be seen here and here. The general drawback of the method is the need to deliberately set a parameter value to 'blank' (as opposed to unset) so that it may act as a switch. Users calling the template will often assume that a parameter set blank is the same as an unset parameter and exclude it... thereby deactivating the conditional feature. This isn't a problem if the blank parameter is set by a template which then calls another template, but then the anti 'meta-template' jihadists take up arms. Ergo... m:ParserFunctions was developed. --CBDunkerson 08:08, 22 April 2006 (UTC)

New twist in the mediazilla:5678 saga, it breaks after substitutions. If I'd want to do something with {{{1|default}}} and need no "else" code, then I'd use the old mechanism, it's harder to destroy it. -- Omniplex 05:34, 16 May 2006 (UTC)

[edit] Interwiki link to vi:

Note to admin handling this: Template talk:! redirects here (which is fine), hence why the request for modification is here and not there.

Please add an interwiki link at Template:! to the Vietnamese version of that template:

[[vi:Tiêu bản:!]]

Thanks.

 – Minh Nguyễn (talk, contribs) 19:50, 8 April 2006 (UTC)

Done. --CBDunkerson 20:36, 8 April 2006 (UTC)
My bad, I didn't expect a protection when I redirected it. -- Omniplex 20:50, 8 April 2006 (UTC)

[edit] m:ParserFunctions

Ok, so we have built-in conditionals (per header) on a trial basis. Unfortunately, they work nearly the same as QIF except for a few issues;

  1. The '#if:' feature does not use 'parameter names'. So, '|test=<condition>|then=<text if true>|else=<text if false>' becomes just '|<condition>|<text if true>|<text if false>'. Overall I think the shorter format is better - though it requires that a user know the order of the parameters rather than being able to set them in any order.
  2. For 'QIF' a '|test=0' condition resolved to 'test exists' and was therefor 'TRUE'. For '#if:' a condition of '0' resolves to 'FALSE'. Again, I think this is an improvement overall. It may change the behaviour of a few templates, but it simplifies the use of conditions based on mathematical and boolean logic.
  3. Finally, #if: seems to have a problem with line breaks that can be somewhat confusing;
This works as expected;
|- valign=top
{{#if:{{{param|}}}|
{{!}} Parameter:
{{!}} {{{param}}}}}

This does not;
|- valign=top {{#if:{{{param|}}}|
{{!}} Parameter:
{{!}} {{{parameter}}}}}

Essentially, #if: ignores the line break after the condition and the second scenario above resolves as;

|- valign=top | Parameter:
| <value of {{{parameter}}}>

Obviously that isn't proper table markup and thus the 'Parameter:' just is not displayed. This is easy enough to resolve by placing the '{{#if:' on a new line, but it is an additional limitation compared to 'QIF'.

Other than those issues I think #if: and QIF are essentially identical in function. You can use HTML tags (i.e. <tr>, <th>, <td>) or the {{!}} template trick - provided you keep the line-break issue in mind, but not direct '|' table markup. It accepts '|' in wiki-links like QIF. Less than we might have hoped for, but I think a viable replacement... and the other new features (expr: & rand:) are wonderful. The #ifeq is similar to the 'booleq' template, but I'll have to do more testing - I suspect it has the same sort of minor quirks as '#if:' vs 'qif'. --CBDunkerson 11:38, 13 April 2006 (UTC)

Something tells me the bit with #if treating "0" as false will bite us more than help us. See my suggestion on m:ParserFunctions for a #ifexpr. If that were implemented, I think #if should be modified to be a simple empty/non-empty test. Or would there still be a good reason for the true/false test? —Locke Coletc 12:10, 13 April 2006 (UTC)
Stuff written by me now documented in mediazilla:5569 removed here, the <pre> bugs are unrelated to {{Qif}} and #if; oddities. -- Omniplex 22:23, 17 April 2006 (UTC)

[edit] Updated instructions

I've updated the instructions to use #if instead of {{qif}}. The only loose-end that remains is updating {{Infobox}} (which is still referenced in these docs) and deciding upon a new name for this page. Originally I planned on moving this to Wikipedia:If conditionals, but maybe Wikipedia:ParserFunctions conditionals would be more appropriate (and would allow for later expansion to include examples using #ifeq, #ifexpr and so forth)? Or perhaps a more generic name, like Wikipedia:Template conditionals? Comments welcome, also, if anyone notices anything I missed in the article, please correct. =) —Locke Coletc 05:47, 17 April 2006 (UTC)

I've added a caveat about "=" and positional parameters. Where {{ifdef}} works (no else needed) it's unnecessary to replace it by #if: (and v.v.). Renaming Wikipedia:Conditional tables (edit talk links history) is tricky, what about all its links and the shortcut? -- Omniplex 23:25, 17 April 2006 (UTC)
The only trouble with renaming this page is handling any double redirects that crop up. I don't think there'll be many though. =) Redirects from here to whatever new name is chosen will handle the rest. —Locke Coletc 16:27, 18 April 2006 (UTC)
Double redirects are shown, but what about dozens of [[w:Wikipedia:Qif conditionals]] on Meta? Maybe I even used [[w:WP:QIF]] or similar there. -- Omniplex 00:47, 22 April 2006 (UTC)
As WP:QIF would be changed to point to the new name (and Wikipedia:Qif conditionals would also point to the new name) it shouldn't matter. —Locke Coletc 01:00, 22 April 2006 (UTC)

[edit] #if: 5678 watch

A condensed variant of the table posted on Talk:ParserFunctions, see also mediazilla:5678. Test cases for the "else" anomaly clobbering the second parameter and comparisons with ifdef and ifndef added, subst test case removed (known to fail with ifndef or qif). The problem is fixed if 2nd and 3rd coloumn show roughly the same result.


[edit] #if: table

Code Result ifdef
{{#if: {{{tst}}} | -1- {{{1}}} }}
-1- {{{1}}} Template:Ifdef
{{#if: {{{tst}}} | -2- {{{1|default}}} }}
-2- default Template:Ifdef
{{#if: {{{tst}}} | -3- {{{2}}} | '''err''' }}
-3- {{{2}}} Template:Ifdef
{{#if: {{{tst}}} | -4- {{{2}}} }}
-4- {{{2}}} Template:Ifdef


[edit] #if: else {{{2}}} anomaly

Code Result ifndef
{{#if: {{ns:0}} |n/a| -1- {{{2}}} }}
-1- {{{2}}} -1- {{{2}}}
{{#if: {{ns:0}} |n/a| -2- {{{2|default}}} }}
-2- default -2- default
{{#if: {{ns:0}} |n/a| -3- {{{1}}} }}
-3- {{{1}}} -3- {{{1}}}
{{#if: {{ns:0}} |n/a| -4- {{{3}}} }}
-4- {{{3}}} -4- {{{3}}}

-- Omniplex 00:28, 28 April 2006 (UTC)

You might want to look at http://test.wikipedia.org/wiki/ParserFunctions and see if the examples there cover the cases you're concerned with. —Locke Coletc 01:13, 28 April 2006 (UTC)
The "Special:Captcha" inline PNGs don't work for me, I only have a Meta-account because Pathoschild created it for me. The backlink at mediazilla:5678 should do it. -- Omniplex 00:38, 1 May 2006 (UTC)

[edit] Use with table formating

I've looked for a way to include formating into a table using #if: I've tryed HTML Table Coding for adding a background colour/image, and i just get the code showing up. And Since i can't use the "|" I can't get Table to have colour or anything else like that.

{{#if: {{{Date of Retirement|}}}|{{{
! colspan="3" style="border:1px solid #a3bfb1; background:#cef2e0;" | Date of Set [[Retirement]] :
| style="border:1px solid #a3bfb1; background:#f5fffa;"|{{{Date of Retirement}}} | }}

The above shows: {{#if: |{Template:! colspan="3" style="border:1px solid

But that has the "|" for wiki tables, so I make them into the

<td><tr><th>

ones.

{{#if: {{{Date of Retirement|}}}|{{{
<th colspan="3" style="border:1px solid #a3bfb1; background:#cef2e0;"> Date of Set [[Retirement]] :</th>
<td style="border:1px solid #a3bfb1; background:#f5fffa;">{{{Date of Retirement}}}</td> | }}

That shows this: {{#if: |{{{

| }}

Could anyone help me with this? I should also mention, I am New at this.

The standard way around this problem is to use {{!}}... this is a template which contains nothing but a '|' character. It is left as a template for the evaluation of the #if:, but then after the conditional logic the | is transcluded in and used normally for table logic. There were a few other issues with the examples (extraneous brackets, no table open/close, blank parameter and thus evaluating to nothing, et cetera) which I adjusted as follows;
{{#if: {{{Date of Retirement|Date}}}|
{{{!}}
! colspan="3" style="border:1px solid #a3bfb1; background:#cef2e0;" {{!}} Date of Set [[Retirement]] :
{{!}} style="border:1px solid #a3bfb1; background:#f5fffa;"{{!}}{{{Date of Retirement|Date}}}
{{!}}}| }}

Shows:

Date of Set Retirement : {{{Date of Retirement}}}
Date of Set Retirement : Date

[edit] Logical and/or question

I have a question I'm hoping someone can help with. Is there any way to do boolean and/or with parameters and #if, short of using nested if blocks? For instance,

{{#if:{{{param1|}}} or {{{param2|}}}|then case|else case}}

Any help would be much appreciated. — George Saliba [talk] 20:40, 5 December 2006 (UTC)

Nevermind, I figured it out. For or at least you just do {{#if:{{{param1|}}}{{{param2|}}}|then case... — George Saliba [talk] 10:50, 6 December 2006 (UTC)

[edit] Help with conditionals with specific variables and if/then

I am working on a template User:Andrew c/New Testament manuscripts. Instead of forcing the user to format numbers in a specific way, I would like the template to automatically code the numbers based on what class they imput. For example, if the class is set to "papyrus" the number function gets put into a superscript. If the class is set to "Uncial", the number gets bolded. If the class is a lectionary, the number gets italicized. You can look through the history to see the sort of things I have tried out. My problem is I don't know how to get the if to recognize a user inputted variable, and I do not know how to combine multiple conditionals. Any help would be greatly appreciated. Thanks.-Andrew c 18:30, 11 May 2007 (UTC)

[edit] Requested move

Wikipedia:Conditional display of table rows makes much more sense, since... er, that's what it's about. (Still not sure what a "qif" is when it's at home...) -- 217.42.77.246 12:02, 4 September 2007 (UTC)

This page is mostly kept for historical purposes since 'qif' has been deprecated and replaced by m:ParserFunctions. Thus, a move to some other name would not make sense as people would only be looking at it based on it's past history of use under this name. Also, the conditional logic introduced by qif could be used for all kinds of conditions... not just conditional display of rows. As to the name itself, I believe it was short-hand for 'quicker if'... the original 'Template:If' conditional method having been slightly more computationally involved than qif. --CBD 13:07, 4 September 2007 (UTC)
Can it at least be marked as historical, then, so that people coming across it for the first time don't start wondering what on earth it is? -- 217.42.77.246 17:30, 4 September 2007 (UTC)
I went to do that... and found that the page has been completely rewritten. It now covers the use of the #if parserFunction for table logic (both columns and rows) rather than Qif at all. Oops, sorry about that. As such I agree that a name change makes sense. How about something like 'Wikipedia:Conditional tables'? --CBD 23:33, 5 September 2007 (UTC)
Sounds OK to me... -- 86.133.138.146 14:47, 7 September 2007 (UTC)