Template talk:Ifold

From Meta, a Wikimedia project coordination wiki

Jump to: navigation, search

Template:ifold containing "{{{else{{{test|}}}|{{{test{{{test|}}}|{{{then|}}}}}}}}}<noinclude>[[Category:Miscellaneous templates|{{PAGENAME}}]]</noinclude>"

Contents

[edit] Description

  • If the value of test is non-empty ("true"), then: if the value of then is defined, that value is produced, otherwise the empty string.
  • If the value of test is empty ("false"), then: if the value of else is defined, that value is produced, otherwise the empty string.
  • If the value of test is undefined:
    • if the value of else is defined, that value is produced.
    • if the value of else is also undefined, then: if the value of then is defined, that value is produced, otherwise the empty string.

Note that the rules for the case that the value of test is undefined are somewhat arbitrary, and just a by-product of the code. Note in particular that in this case it makes a difference whether the value of else is empty or undefined, see the examples below.

[edit] Examples

Overview of all 12 cases:

Code Result
{{Ifold|test=1|then=a|else=b}} a
{{Ifold|test=|then=a|else=b}} b
{{Ifold|test=1|then=a}} a
{{Ifold|test=|then=a}}
{{Ifold|test=1|else=b}}
{{Ifold|test=|else=b}} b
{{Ifold|test=1}}
{{Ifold|test=}}
{{Ifold|then=a|else=b}} b
{{Ifold|then=a}} a
{{Ifold|else=b}} b
{{Ifold}}

Example of other test value:

Code Result
{{Ifold|test=2|then=a|else=b}} a

{{ifold|test=1|then=abc{{t|def}}ghi|else=jkl}} using template:t ( talk edit history links ) gives abcstart-def-endghi

{{ifold|test=|then=abc{{t|def}}ghi|else=jkl}} gives jkl

{{ifold|test={{equal||a}}|then={{t|a}}|else={{t2|a|b}}}} gives parameter 1 is "a", parameter 2 is "b"

{{ifold|test={{equal||}}|then={{t|}}|else={{t2||b}}}} gives start--end

{{ifold|test={{not equal||v}}|then={{death|[[November 2]], [[2004]]|[[Amsterdam]]}}}} gives:

Date of death: November 2, 2004, place of death: [[ Amsterdam]]

{{ifold|test={{not equal||}}|then={{death|[[November 2]], [[2004]]|[[Amsterdam]]}}}} gives ""

Examples with only an else-part:

{{ifold|test={{equal||v}}|else={{death|[[November 2]], [[2004]]|[[Amsterdam]]}} gives:

Date of death: November 2, 2004, place of death: [[ Amsterdam]]

{{ifold|test={{equal||}}|else={{death|[[November 2]], [[2004]]|[[Amsterdam]]}} gives ""

"This is {{ifold|test={{not equal|User|{{NAMESPACE}}}}|then=not}} the User namespace." gives:

This is not the User namespace.

Note that {{NAMESPACE}} produces underscores for spaces, which are not considered equal by the template:

{{ifold|test={{equal|{{NAMESPACE}}|Template_talk}}|then=yes|else=no}} gives no

{{ifold|test={{equal|{{NAMESPACE}}|Template talk}}|then=yes|else=no}} gives yes

[edit] Comparison

The template is a copy of en:template:qif ( talk edit history links ).

The template is more versatile than template:If equal g ( talk edit history links ), etc.

Description of what that does:

If the 3rd and 4th parameter are equal than call the template specified by the 1st parameter, otherwise that by the 2nd. Each can have up to three unnamed parameters specified as the 5th, 6th and 7th parameter.

For each value of the 3rd parameter an auxiliary template callpar is needed, with the content {{{{{par }}}|{{{1}}}|{{{2}}}|{{{3}}}}}

Corresponding examples:

*{{if equal g|t|t2||a|a|b|c}}
*{{if equal g|t|t2||||b|c}}
*{{if equal g|x0|death||v|[[November 2]], [[2004]]|[[Amsterdam]]|}}
*{{if equal g|x0|death|||||}}
*This is {{if equal g|x0|x1|User|{{NAMESPACE}}|not||}} the User namespace.

give:

  • parameter 1 is "a", parameter 2 is "b"
  • start--end
  • Date of death: November 2, 2004, place of death: Amsterdam
  • This is not the User namespace.

and:

  • {{if equal g|x2|x3|{{NAMESPACE}}|Template_talk|a||}} gives aa
  • {{if equal g|x2|x3|{{NAMESPACE}}|Template talk|a||}} gives {{{{{Template_talk}}}|a||}}

[edit] Direct use of the content of the template

If one wants to avoid nested templates to reduce server strain, one can also put the code of the template directly in some other template, e.g. an infobox.

If no "else" part is needed it can be reduced to:

{{{test{{{test|}}}|{{{then|}}}}}}

In that case the description reduces to:

  • If the value of test is non-empty ("true") or undefined, then: if the value of then is defined, that value is produced, otherwise the empty string.
  • If the value of test is empty ("false"), then the empty string is produced.

[edit] Caveats

Using "|" (&#124;) for Wiki tables in conjunction with Template:ifold can be difficult, this is discussed in w:Wikipedia:Conditional tables. One workaround is to escape "|" by template:! ( talk edit history links ).

[edit] See also