Template:If

From MHWiki
Revision as of 07:03, 15 June 2021 by B.Tay (talk | contribs) (Corrected typo in second example and rewrote the last paragraph)
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".

One extra point that you should be aware of: {{NAMESPACE}} expands as empty (or no value) when the page is in the main namespace. It may be a little bit confusing in terms of intuition to see the second example above where the category tagging occurs as the result in the spot usually reserved for "if empty" since most conditional tests are designed to do something given the presence of text, though this is one example where the conditional test is designed to do something in the absence of text.