Template:Ifparadef: Difference between revisions

From Avlis Wiki
Jump to navigation Jump to search
(Template:Ifparadef (If parameter defined) - to test if a parameter is defined.)
 
m (Fixing pre/table line wrap style and table width.)
 
Line 26: Line 26:
* Extended functionality: <code><nowiki>{{ifparadef | {{{parameter_name|¬}}} | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>
* Extended functionality: <code><nowiki>{{ifparadef | {{{parameter_name|¬}}} | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>


The first parameter, which is required, is the name of the parameter to be tested. It must be formatted as <code><nowiki>{{{parameter_name |¬}}}</nowiki></code>, '''with the "¬"''', otherwise the template will not work correctly (see the [[#Examples | Examples]] section below for examples with and without the "¬").
The first parameter, which is required, is the name of the parameter to be tested. It must be formatted as <code><nowiki>{{{parameter_name|¬}}}</nowiki></code>, '''with the "¬"''', otherwise the template will not work correctly (see the [[#Examples | Examples]] 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 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.
Line 39: Line 39:
==== Examples ====
==== Examples ====


{| class="wikitable" style="width: 98%;"
{| class="wikitable" style="white-space: pre; width: 98%;"
|+ Normal use
|+ style="white-space: pre-wrap;" | Normal use
|-
|-
! Code !! Result
! Code !! Result
Line 57: Line 57:
|-
|-
|}
|}
{| class="wikitable" style="width: 98%;"
{| class="wikitable" style="white-space: pre; width: 98%;"
|+ Extended use (with fourth parameter)
|+ style="white-space: pre-wrap;" | Extended use (with fourth parameter)
|-
|-
! Code !! Result
! Code !! Result
Line 77: Line 77:
| {{ifparadef |  | Defined, not empty | Not defined | Defined, empty}}
| {{ifparadef |  | Defined, not empty | Not defined | Defined, empty}}
|}
|}
{| class="wikitable" style="width: 98%;"
{| class="wikitable" style="white-space: pre; width: 98%;"
|+ <span style="color:red">'''Incorrect'''</span> <nowiki>{{ifparadef}}</nowiki> usage in a template named <nowiki>{{ifparadef/test}}</nowiki>, containing: <code><nowiki>{{ifparadef | {{{parameter|}}} | Defined | Not defined}}</nowiki></code>
|+ style="white-space: pre-wrap;" | <span style="color:red">'''Incorrect'''</span> <nowiki>{{ifparadef}}</nowiki> usage in a template named <nowiki>{{ifparadef/test}}</nowiki>, containing: <code><nowiki>{{ifparadef&nbsp;|&nbsp;{{{parameter|}}}&nbsp;|&nbsp;Defined&nbsp;|&nbsp;Not&nbsp;defined}}</nowiki></code>
|-
|-
! Code !! Result
! Code !! Result
Line 91: Line 91:
| {{ifparadef | | Defined | Not defined}}
| {{ifparadef | | Defined | Not defined}}
|}
|}
{| class="wikitable" style="width: 98%;"
{| class="wikitable" style="white-space: pre; width: 98%;"
|+ <span style="color:green">'''Correct'''</span> <nowiki>{{ifparadef}}</nowiki> usage in a template named <nowiki>{{ifparadef/test2}}</nowiki>, containing: <code><nowiki>{{ifparadef | {{{parameter|¬}}} | Defined | Not defined}}</nowiki></code>
|+ style="white-space: pre-wrap;" | <span style="color:green">'''Correct'''</span> <nowiki>{{ifparadef}}</nowiki> usage in a template named <nowiki>{{ifparadef/test2}}</nowiki>, containing: <code><nowiki>{{ifparadef&nbsp;|&nbsp;{{{parameter|¬}}}&nbsp;|&nbsp;Defined&nbsp;|&nbsp;Not&nbsp;defined}}</nowiki></code>
|-
|-
! Code !! Result
! Code !! Result
Line 105: Line 105:
| {{ifparadef | ¬ | Defined | Not defined}}
| {{ifparadef | ¬ | Defined | Not defined}}
|}
|}
{| class="wikitable" style="width: 98%;"
{| class="wikitable" style="white-space: pre; width: 98%;"
|+ <span style="color:red">'''Incorrect'''</span> <nowiki>{{ifparadef}}</nowiki> usage in a template named <nowiki>{{ifparadef/test3}}</nowiki>, containing: <code><nowiki>{{ifparadef | {{{parameter|}}} | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>
|+ style="white-space: pre-wrap;" | <span style="color:red">'''Incorrect'''</span> <nowiki>{{ifparadef}}</nowiki> usage in a template named <nowiki>{{ifparadef/test3}}</nowiki>, containing: <code><nowiki>{{ifparadef&nbsp;|&nbsp;{{{parameter|}}}&nbsp;|&nbsp;Defined,&nbsp;not&nbsp;empty&nbsp;|&nbsp;Not&nbsp;defined&nbsp;|&nbsp;Defined,&nbsp;empty}}</nowiki></code>
|-
|-
! Code !! Result
! Code !! Result
Line 119: Line 119:
| {{ifparadef | | Defined, not empty | Not defined | Defined, empty}}
| {{ifparadef | | Defined, not empty | Not defined | Defined, empty}}
|}
|}
{| class="wikitable" style="width: 98%;"
{| class="wikitable" style="white-space: pre; width: 98%;"
|+ <span style="color:green">'''Correct'''</span> <nowiki>{{ifparadef}}</nowiki> usage in a template named <nowiki>{{ifparadef/test4}}</nowiki>, containing: <code><nowiki>{{ifparadef | {{{parameter|¬}}} | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>
|+ style="white-space: pre-wrap;" | <span style="color:green">'''Correct'''</span> <nowiki>{{ifparadef}}</nowiki> usage in a template named <nowiki>{{ifparadef/test4}}</nowiki>, containing: <code><nowiki>{{ifparadef&nbsp;|&nbsp;{{{parameter|¬}}}&nbsp;|&nbsp;Defined,&nbsp;not&nbsp;empty&nbsp;|&nbsp;Not&nbsp;defined&nbsp;|&nbsp;Defined,&nbsp;empty}}</nowiki></code>
|-
|-
! Code !! Result
! Code !! Result
Line 140: Line 140:


<pre style="white-space: pre">
<pre style="white-space: pre">
{{#ifeq: {{{parameter_name|¬}}} |¬
{{#ifeq: {{{parameter_name|¬}}} | ¬
| Not defined.
| Not defined.
| Defined. (Empty or has data.)
| Defined. (Empty or has data.)
Line 155: Line 155:
For code that works with any input (detects "¬" as defined, not empty), see [[Wikipedia:Template:ifparadef full#Hardcoding | Template:Ifparadef full#Hardcoding]]. Use the code below for standard and extended functionality:
For code that works with any input (detects "¬" as defined, not empty), see [[Wikipedia:Template:ifparadef full#Hardcoding | Template:Ifparadef full#Hardcoding]]. Use the code below for standard and extended functionality:


<pre style="white-space: pre-wrap">
<pre style="white-space: pre">
{{#ifeq: {{{parameter_name|x}}} | {{{parameter_name|y}}}
{{#ifeq: {{{parameter_name|x}}} | {{{parameter_name|y}}}
| Defined. (Empty or has data.)
| Defined. (Empty or has data.)

Latest revision as of 05:32, 6 February 2017


Template:Ifparadef (If parameter defined)

This template is based on Template:Ifparadef.

It uses the original markup. The documentation below was copied to this template, since we do not use documentation sub-pages. Most of the Wikipedia specific information, links and sub-templates have either been removed, or modified for the Avlis wiki.

See the original template documentation for further details.


Template documentation

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


Usage

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 Examples 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}}, or the Hardcoding examples below.


Examples

Normal use
Code Result
{{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)
Code 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
Incorrect {{ifparadef}} usage in a template named {{ifparadef/test}}, containing: {{ifparadef | {{{parameter|}}} | Defined | Not defined}}
Code Result
{{ifparadef/test | parameter = Some text}} Defined
{{ifparadef/test | parameter = }} Defined
{{ifparadef/test}} Defined
Correct {{ifparadef}} usage in a template named {{ifparadef/test2}}, containing: {{ifparadef | {{{parameter|¬}}} | Defined | Not defined}}
Code Result
{{ifparadef/test2 | parameter = Some text}} Defined
{{ifparadef/test2 | parameter = }} Defined
{{ifparadef/test2}} Not defined
Incorrect {{ifparadef}} usage in a template named {{ifparadef/test3}}, containing: {{ifparadef | {{{parameter|}}} | Defined, not empty | Not defined | Defined, empty}}
Code Result
{{ifparadef/test3 | parameter = Some text}} Defined, not empty
{{ifparadef/test3 | parameter = }} Defined, empty
{{ifparadef/test3}} Defined, empty
Correct {{ifparadef}} usage in a template named {{ifparadef/test4}}, containing: {{ifparadef | {{{parameter|¬}}} | Defined, not empty | Not defined | Defined, empty}}
Code Result
{{ifparadef/test4 | parameter = Some text}} Defined, not empty
{{ifparadef/test4 | parameter = }} Defined, empty
{{ifparadef/test4}} Not defined


Hardcoding

An alternative to using this meta-template is to hardcode this functionality in your template. Use the code below for standard and 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. Use the code below for standard and extended functionality:

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

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


See also

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