Difference between revisions of "Template:If"

From MHWiki
(Graduate to main template namespace from User:Ericb/If)
 
m (fix example for namespace tagging)
Line 7: Line 7:
 
  <nowiki>{{if|{{{1|}}}|[[{{{1}}}]]|none}}</nowiki>
 
  <nowiki>{{if|{{{1|}}}|[[{{{1}}}]]|none}}</nowiki>
 
results in either parameter 1 converted to a link, or the literal text "none"
 
results in either parameter 1 converted to a link, or the literal text "none"
  <nowiki>{{if|{{{NAMESPACE}}}|[[Category:blah]]}}</nowiki>
+
  <nowiki>{{if|{{{NAMESPACE}}}||[[Category:blah]]}}</nowiki>
 
tags a page with a category, but only if the page is in the main namespace
 
tags a page with a category, but only if the page is in the main namespace
 
</noinclude><includeonly>{{use|use={{{2|}}}|use{{{1|}}}={{{3|}}}}}</includeonly>
 
</noinclude><includeonly>{{use|use={{{2|}}}|use{{{1|}}}={{{3|}}}}}</includeonly>

Revision as of 16:27, 18 May 2015

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 the {{Use}} template 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