User:Gorgon/Sandbox/Sandbox Charlie: Difference between revisions

From Avlis Wiki
Jump to navigation Jump to search
m (Test version of emplate:Ifparadef (If parameter defined).)
m (Fixing my over-zealous search/replace spacing inside parameter values.)
Line 23: Line 23:


This template accepts up to four unnamed parameters.
This template accepts up to four unnamed parameters.
* Standard functionality: <code><nowiki>{{ifparadef | {{{parameter_name | ¬}}} | Defined | Not defined}}</nowiki></code>
* Standard functionality: <code><nowiki>{{ifparadef | {{{parameter_name|¬}}} | Defined | Not defined}}</nowiki></code>
* 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 44: Line 44:
! Code !! Result
! Code !! Result
|-
|-
| <code><nowiki>{{ifparadef | {{{parameter_name | ¬}}} | Defined | Not defined}}</nowiki></code>
| <code><nowiki>{{ifparadef | {{{parameter_name|¬}}} | Defined | Not defined}}</nowiki></code>
| {{<!-- ifparadef -->User:Gorgon/Sandbox/Sandbox Charlie | {{{parameter_name | ¬}}} | Defined | Not defined}}
| {{<!-- ifparadef -->User:Gorgon/Sandbox/Sandbox Charlie | {{{parameter_name|¬}}} | Defined | Not defined}}
|-
|-
| <code><nowiki>{{ifparadef | {{{parameter_name | Some text}}} | Defined | Not defined}}</nowiki></code>
| <code><nowiki>{{ifparadef | {{{parameter_name|Some text}}} | Defined | Not defined}}</nowiki></code>
| {{<!-- ifparadef -->User:Gorgon/Sandbox/Sandbox Charlie | {{{parameter_name | Some text}}} | Defined | Not defined}}
| {{<!-- ifparadef -->User:Gorgon/Sandbox/Sandbox Charlie | {{{parameter_name|Some text}}} | Defined | Not defined}}
|-
|-
| <code><nowiki>{{ifparadef | Some text | Defined | Not defined}}</nowiki></code>
| <code><nowiki>{{ifparadef | Some text | Defined | Not defined}}</nowiki></code>
Line 62: Line 62:
! Code !! Result
! Code !! Result
|-
|-
| <code><nowiki>{{ifparadef | {{{parameter_name | }}} | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>
| <code><nowiki>{{ifparadef | {{{parameter_name|}}} | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>
| {{<!-- ifparadef -->User:Gorgon/Sandbox/Sandbox Charlie | {{{parameter_name | }}} | Defined, not empty | Not defined | Defined, empty}}
| {{<!-- ifparadef -->User:Gorgon/Sandbox/Sandbox Charlie | {{{parameter_name|}}} | Defined, not empty | Not defined | Defined, empty}}
|-
|-
| <code><nowiki>{{ifparadef | {{{parameter_name | Some text}}} | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>
| <code><nowiki>{{ifparadef | {{{parameter_name|Some text}}} | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>
| {{<!-- ifparadef -->User:Gorgon/Sandbox/Sandbox Charlie | {{{parameter_name | Some text}}} | Defined, not empty | Not defined | Defined, empty}}
| {{<!-- ifparadef -->User:Gorgon/Sandbox/Sandbox Charlie | {{{parameter_name|Some text}}} | Defined, not empty | Not defined | Defined, empty}}
|-
|-
| <code><nowiki>{{ifparadef | {{{parameter_name | ¬}}} | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>
| <code><nowiki>{{ifparadef | {{{parameter_name|¬}}} | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>
| {{<!-- ifparadef -->User:Gorgon/Sandbox/Sandbox Charlie | {{{parameter_name | ¬}}} | Defined, not empty | Not defined | Defined, empty}}
| {{<!-- ifparadef -->User:Gorgon/Sandbox/Sandbox Charlie | {{{parameter_name|¬}}} | Defined, not empty | Not defined | Defined, empty}}
|-
|-
| <code><nowiki>{{ifparadef | Some text | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>
| <code><nowiki>{{ifparadef | Some text | Defined, not empty | Not defined | Defined, empty}}</nowiki></code>
Line 78: Line 78:
|}
|}
{| class="wikitable" style="width: 98%;"
{| class="wikitable" style="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>
|+ <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>
|-
|-
! Code !! Result
! Code !! Result
Line 92: Line 92:
|}
|}
{| class="wikitable" style="width: 98%;"
{| class="wikitable" style="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>
|+ <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>
|-
|-
! Code !! Result
! Code !! Result
Line 106: Line 106:
|}
|}
{| class="wikitable" style="width: 98%;"
{| class="wikitable" style="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>
|+ <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>
|-
|-
! Code !! Result
! Code !! Result
Line 120: Line 120:
|}
|}
{| class="wikitable" style="width: 98%;"
{| class="wikitable" style="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>
|+ <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>
|-
|-
! 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.)
}}
}}


{{#switch: {{{parameter_name | ¬}}}
{{#switch: {{{parameter_name|¬}}}
| ¬ = Not defined.
| ¬ = Not defined.
|  = Defined, empty.
|  = Defined, empty.
Line 156: Line 156:


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


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

Revision as of 23:04, 2 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}}.


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.