Template:Ifold/doc

From Meta, a Wikimedia project coordination wiki
Jump to: navigation, search

Template:ifold containing "{{{else{{{test|}}}|{{{test{{{test|}}}|{{{then|}}}}}}}}}<noinclude>{{documentation}}</noinclude>"

Description[edit]

  • 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.

Examples[edit]

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 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 start-a-end

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

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


{{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:


{{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 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 yes

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

Comparison[edit]

The template is a copy of w:en:Template:Qif.

The template is more versatile than Template:If equal g, 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:

and:

Direct use of the content of the template[edit]

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.

Caveats[edit]

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:!.

See also[edit]