Difference between revisions of "Template:If"
From MHWiki
m (Corrected typo in second example and rewrote the last paragraph) |
m (Adjustments made to 'Further Illustration' portion for readability) |
||
Line 3: | Line 3: | ||
Overview: | Overview: | ||
− | A conditional template, for testing if text is empty. The first argument cannot start with "=". If the first argument contains any text, then this ends up calling the [[Template:Use]] template with two arguments, one named "use" with the original second argument as its value, and the other will have an unrelated name, so the overall expansion is the one use of the name "use" - the second argument. But if the first argument is empty (or missing), then this ends up using [[Template:Use]] with "use" specified twice, and the last use wins, giving the overall expansion of the third argument. That is, this template is an alternative implementation of [https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##if <nowiki>{{ | + | A conditional template, for testing if text is empty. The first argument cannot start with "=". If the first argument contains any text, then this ends up calling the [[Template:Use]] template with two arguments, one named "use" with the original second argument as its value, and the other will have an unrelated name, so the overall expansion is the one use of the name "use" - the second argument. But if the first argument is empty (or missing), then this ends up using [[Template:Use]] with "use" specified twice, and the last use wins, giving the overall expansion of the third argument. That is, this template is an alternative implementation of [https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##if <nowiki>{{#if:test|if text|if empty}}</nowiki>], with a slightly different syntax: <nowiki>{{if|test|if text|if empty}}</nowiki>. |
Examples: | Examples: | ||
Line 13: | Line 13: | ||
Further illustration: It is '''assumed''' in this section that you understand how [[Template:Use]] works. You are also '''assumed''' to have generally understood how [https://www.mediawiki.org/wiki/Help:Templates#Parameters Templates#Parameters] work, in particular the sections on numbered parameters and default values. | Further illustration: It is '''assumed''' in this section that you understand how [[Template:Use]] works. You are also '''assumed''' to have generally understood how [https://www.mediawiki.org/wiki/Help:Templates#Parameters Templates#Parameters] work, in particular the sections on numbered parameters and default values. | ||
− | The base code used in this function is: <nowiki>{{use|use={{{2|}}}|use{{{1|}}}={{{3|}}}}}</nowiki>. | + | The base code used in this function is: <nowiki>{{use|use={{{2|}}}|use{{{1|}}}={{{3|}}}}}</nowiki> |
+ | |||
+ | Consider the following example (from the syntax above): <nowiki>{{if|test|if text|if empty}}</nowiki> | ||
+ | |||
+ | For this example, "1" is defined as "test", "2" is defined as "if text" and "3" is defined as "if empty". Substituting these definitions into the base code, we have: | ||
+ | |||
+ | <nowiki>{{use|use={{{if text|}}}|use{{{test|}}}={{{if empty|}}}}}</nowiki> | ||
− | + | We now have to consider two scenarios: 1) When "test" has text (let's call this text "defined" for instance), 2) When "test" is empty. | |
− | + | ||
+ | 1) When "test" has text, the base code now changes to: | ||
+ | |||
+ | <nowiki>{{use|use={{{if text|}}}|usedefined={{{if empty|}}}}}</nowiki> | ||
+ | |||
+ | With "usedefined" not being "indir" or "use" (notice how any text in principle will result in some parameter that is never those two), it'll simply be ignored when the "use" parameter is expanded, giving the final value of "if text". | ||
+ | |||
+ | 2) When "test" is empty, the base code now changes to: | ||
− | + | <nowiki>{{use|use={{{if text|}}}|use={{{if empty|}}}}}</nowiki> | |
− | |||
− | + | With there being duplicate "use" parameters, the latter value will be used when the "use" parameter is expanded, giving the final value of "if empty". | |
</noinclude><includeonly>{{use|use={{{2|}}}|use{{{1|}}}={{{3|}}}}}</includeonly> | </noinclude><includeonly>{{use|use={{{2|}}}|use{{{1|}}}={{{3|}}}}}</includeonly> |
Latest revision as of 15:47, 19 June 2021
- To Editors: If you have come to this page to find out why there is a warning in your Edit page, see Template:Use#Warning Explanation.
Overview:
A conditional template, for testing if text is empty. The first argument cannot start with "=". If the first argument contains any text, then this ends up calling the Template:Use template with two arguments, one named "use" with the original second argument as its value, and the other will have an unrelated name, so the overall expansion is the one use of the name "use" - the second argument. But if the first argument is empty (or missing), then this ends up using Template:Use with "use" specified twice, and the last use wins, giving the overall expansion of the third argument. That is, this template is an alternative implementation of {{#if:test|if text|if empty}}, with a slightly different syntax: {{if|test|if text|if empty}}.
Examples:
{{if|{{{1|}}}|[[{{{1}}}]]|none}} results in either parameter 1 converted to a link, or the literal text "none"
{{if|{{NAMESPACE}}||[[Category:blah]]}} tags a page with a category, but only if the page is in the main namespace
Further illustration: It is assumed in this section that you understand how Template:Use works. You are also assumed to have generally understood how Templates#Parameters work, in particular the sections on numbered parameters and default values.
The base code used in this function is: {{use|use={{{2|}}}|use{{{1|}}}={{{3|}}}}}
Consider the following example (from the syntax above): {{if|test|if text|if empty}} For this example, "1" is defined as "test", "2" is defined as "if text" and "3" is defined as "if empty". Substituting these definitions into the base code, we have: {{use|use={{{if text|}}}|use{{{test|}}}={{{if empty|}}}}} We now have to consider two scenarios: 1) When "test" has text (let's call this text "defined" for instance), 2) When "test" is empty.
1) When "test" has text, the base code now changes to: {{use|use={{{if text|}}}|usedefined={{{if empty|}}}}} With "usedefined" not being "indir" or "use" (notice how any text in principle will result in some parameter that is never those two), it'll simply be ignored when the "use" parameter is expanded, giving the final value of "if text".
2) When "test" is empty, the base code now changes to: {{use|use={{{if text|}}}|use={{{if empty|}}}}} With there being duplicate "use" parameters, the latter value will be used when the "use" parameter is expanded, giving the final value of "if empty".