Vés al contingut

Plantilla:Ifparadef

De la Viquipèdia, l'enciclopèdia lliure


Icona de documentació de mòdul Documentació de la plantilla [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]

This is the {{ifparadef}} meta-template.

This template is used to test if a parameter is defined; thus, it should only be directly used on other templates.

This template accepts up to four unnamed parameters.

  • Standard functionality: {{ifparadef|{{{parameter_name|¬}}}|Defined|Not defined}}
  • Extended functionality: {{ifparadef|{{{parameter_name|¬}}}|Defined, not empty|Not defined|Defined, empty}}

The first parameter, which is required, is the name of the parameter to be tested. It must be formatted as {{{parameter_name|¬}}}, with the "¬", otherwise the template will not work correctly (see the Exemples section below for examples with and without the "¬").

The second parameter is the text to display if the tested parameter is defined. If the fourth parameter is also used, the second parameter is the text displayed when the tested parameter is defined and not empty.

The third parameter is the text to display if the tested parameter is not defined.

The fourth parameter is the text to display if the tested parameter is defined and empty (or if it contains only whitespace). If this parameter is omitted, only one defined test is performed, and the contents (or default) of the second parameter are used as long as the tested parameter is defined.

This template uses "¬" to test for a defined parameter; the template cannot detect when a literal "¬" is passed as input from a final deployment (such as an article) and will give incorrect results in such cases. If you need to handle that case too, then see {{ifparadef full}}.

Exemples

[modifica]
Ús normal
Codi Resultat
{{ifparadef|{{{parameter_name|¬}}}|Defined|Not defined}} Not defined
{{ifparadef|{{{parameter_name|Some text}}}|Defined|Not defined}} Defined
{{ifparadef|Some text|Defined|Not defined}} Defined
{{ifparadef||Defined|Not defined}} Defined
Extended use (with fourth parameter)
Codi Result
{{ifparadef|{{{parameter_name|}}}|Defined, not empty|Not defined|Defined, empty}} Defined, empty
{{ifparadef|{{{parameter_name|Some text}}}|Defined, not empty|Not defined|Defined, empty}} Defined, not empty
{{ifparadef|{{{parameter_name|¬}}}|Defined, not empty|Not defined|Defined, empty}} Not defined
{{ifparadef|Some text|Defined, not empty|Not defined|Defined, empty}} Defined, not empty
{{ifparadef||Defined, not empty|Not defined|Defined, empty}} Defined, empty
{{ifparadef/test}} (incorrect, containing {{ifparadef|{{{parameter|}}}|Defined|Not defined}})
Codi Resultat
{{ifparadef/test|parameter=Some text}} Plantilla:Ifparadef/test
{{ifparadef/test|parameter=}} Plantilla:Ifparadef/test
{{ifparadef/test}} Plantilla:Ifparadef/test
{{ifparadef/test2}} (correct, containing {{ifparadef|{{{parameter|¬}}}|Defined|Not defined}})
Codi Resultat
{{ifparadef/test2|parameter=Some text}} Plantilla:Ifparadef/test2
{{ifparadef/test2|parameter=}} Plantilla:Ifparadef/test2
{{ifparadef/test2}} Plantilla:Ifparadef/test2
{{ifparadef/test3}} (incorrect, containing {{ifparadef|{{{parameter|}}}|Defined, not empty|Not defined|Defined, empty}})
Codi Resultat
{{ifparadef/test3|parameter=Some text}} Plantilla:Ifparadef/test3
{{ifparadef/test3|parameter=}} Plantilla:Ifparadef/test3
{{ifparadef/test3}} Plantilla:Ifparadef/test3
{{ifparadef/test4}} (correct, containing {{ifparadef|{{{parameter|¬}}}|Defined, not empty|Not defined|Defined, empty}})
Codi Resultat
{{ifparadef/test4|parameter=Some text}} Plantilla:Ifparadef/test4
{{ifparadef/test4|parameter=}} Plantilla:Ifparadef/test4
{{ifparadef/test4}} Plantilla:Ifparadef/test4

Hardcoding

[modifica]

An alternative to using this meta-template is to hardcode this functionality in your template. Here are code for the standard and the extended functionality:

{{#ifeq: {{{parameter_name|¬}}} | ¬
| Not defined.
| Defined. (Empty or has data.)
}}

{{#switch: {{{parameter_name|¬}}}
| ¬ = Not defined.
|   = Defined, empty.
| #default = Defined, not empty.
}}

For code that works with any input (detects "¬" as defined, not empty), see Template:Ifparadef full#Hardcoding.

Vegeu també

[modifica]
  • {{ifparadef full}} - Extended version of this template, but works with any input (correctly detects "¬" as defined, not empty).
  • {{ifempty}} - Allows testing of up to four parameters, returning the contents of the first non-empty one.