Difference between revisions of "Template talk:IfEmpty"

From RPGnet
Jump to: navigation, search
(Created page with "Since wiki.rpg.new does not support the #if statement, this template provides "if like" functionality using a trick in the template engine. To utilize this template, type: <n...")
 
 
Line 3: Line 3:
 
<nowiki>{{IfEmpty|{{{NameOfKey}}}=text that should appear when NameOfKey is empty|text that should appear when NameOfKey has a value}}</nowiki>
 
<nowiki>{{IfEmpty|{{{NameOfKey}}}=text that should appear when NameOfKey is empty|text that should appear when NameOfKey has a value}}</nowiki>
  
This logic works because IfEmpty outputs a single parameter, which is the empty string. If it is not present, then it outputs the first non-named parameter. The trick is that we pass through the **value** of the key that we want to analyze as a named parameter. So when the key has an empty value, you get:
+
This logic works because IfEmpty outputs a single parameter, which is the empty string. If it is not present, then it outputs the first non-named parameter. The trick is that we pass through the ''value'' of the key that we want to analyze as a named parameter. So when the key has an empty value, you get:
  
 
{|
 
{|
Line 24: Line 24:
  
  
The only further trick is that every key used this way must be defined in the template. If it's not present, then the trick doesn't work.
+
The only further trick is that every key used this way must be ''defined'' in the template. If it's not present, then the trick doesn't work. So just define every key, but leave the ones that you aren't using blank, and IfEmpty will work well.

Latest revision as of 12:01, 1 July 2014

Since wiki.rpg.new does not support the #if statement, this template provides "if like" functionality using a trick in the template engine. To utilize this template, type:

{{IfEmpty|{{{NameOfKey}}}=text that should appear when NameOfKey is empty|text that should appear when NameOfKey has a value}}

This logic works because IfEmpty outputs a single parameter, which is the empty string. If it is not present, then it outputs the first non-named parameter. The trick is that we pass through the value of the key that we want to analyze as a named parameter. So when the key has an empty value, you get:

=text that should appear when NameOfKey is empty
1=text that should appear when NameOfKey has a value

Because the empty string key is present, 'text that should appear when NameOfKey is empty' is outputted. However, if NameOfKey has a value, then IfEmpty is called with:

the value stored in NameOfKey=text that should appear when NameOfKey is empty
1=text that should appear when NameOfKey has a value

Because the empty string key is now missing, we output 'text that should appear when NameOfKey has a value'.


The only further trick is that every key used this way must be defined in the template. If it's not present, then the trick doesn't work. So just define every key, but leave the ones that you aren't using blank, and IfEmpty will work well.