User blog:LucyKuranSKYDOME/Magic Words Guide

Have you ever wondered how people write scripts that work on very different pages? Or maybe you have a few templates that have the same purpose and you would like to combine them into one? In this guide we are going to look at the magic words if, ifexpr , ifeq , switch and section.

Syntax
All of the red words mentioned above are considered ParserFunctions that have very specific usage. To put it simply, those are special words inside special symbols that perform magic outside Hogwarts. Calling them on a page requires putting them inside  (for example,   ), which basically means the following: 'Hello, Fandom, right now I am not using a plain text, please be ready to use some functionality instead (see my  .

IF
if is always a condition, if one thing happens this way, another thing happens that way. It usually has 3 bits of information inside the curly brackets:
 * what Fandom needs to  look for ,
 * what to do if this thing  is  found,
 * what to do if this thing  is not  found.

A simple example would be. This basically means 'if the data parameter is filled in, say that there is something, but if it is not filled in, say that there is nothing'.

It can often be used in citation templates, for example, to say 'if the author of the quote is given, display it, if not, then ignore it'.

Instead of using specific placeholder names (like ), it is also common to use the numbers of parameters (like  ). When a template is called on the page (let us use the quotation template as an example here), we can use it as "Text here" . But if we always use the same logic, we might as well remove the placeholders and use it just as "Text here"

- Author here . And for the second case we would use parameter numbers.

This kind of code would tell Fandom 'if you see that there is something written on the second place where the author has to be, display it, if you see nothing, then say that the author is unknown'.

IF in tables
This is probably one of the most underestimated uses of the if function. Let us say we have a template that puts some necessary data in a table that is used on a hundred pages, and the content of this table might vary from one page to another. Sometimes we have 3 rows, sometimes 2. And sometimes 20. So what? Do we build 100 templates with a different number of rows? Just not filling in a placeholder will not work with tables as easily as it works with infoboxes. Even if a row is empty, it will appear on the page no matter what. How do we fix this situation with one little if ?

Here comes the magic: 'Hello, Fandom, how about we style this row in a hacky way? I want you to check if I filled in my   placeholder on the page, and if yes, then everything is cool, ignore me. But if I did not do it, could you please ignore this row instead? Hide it until I fill in my placeholder, thank you!' Right before the necessary row we use |- style="   ". Did you notice the | | instead of another condition? When a condition is not set and is just a white space, it is a signal to Fandom that when the condition is true (in this case, the is filled in on the page and has some value in it), no action is needed there, so Fandom will just ignore this part of the code and move on.

Here are 2 examples from Fractured Online Wiki that use the same template for Loot which can be obtained from various mobs (and when fewer items are dropped, the table gets smaller automatically):

IFEXPR
ifexpr is an extension of if that uses an expression in the condition part, that is why it is called 'if expression' shortened to 'ifexpr'. Simply speaking, it is the same as if, but while if checks whether the word exists and does something depending on it, ifexpr focuses on mathematical usage of this magic function.

Using we will get yes (because 2>1=true). And writing will give us no (because 2<1=false). When can it be helpful? There is a great example on the Spanish Wiki YouTube Pedia that uses ifexpr to add categories automatically! This is the code their template has: Let us see what is does.
 * 1) First of all, we check whether the third parameter given for this template on the page is a number that is strictly smaller than 1981 (for example, if someone was born on |1|12|1979, Fandom will take the third parameter, which is 1979, and compare it to 1981).
 * 2) If this equation is true (and 1979<1981 which it is), this page will get a new category called 'Nacidos antes de 1980' (Born before 1980). NOTE: Since the number must be strictly smaller than 1981, we do not include this number into our logic, so the last date that can be true is 1980.
 * 3) If the year of birth is 1981, and the equation 1981<1981 is not true (which it is obviously not), Fandom will look at the last part of our ifexpr and see that we want it to add 'Nacidos en' (Born in) + which is the year that we stated on the page. In this case (if we were born in 1981), our page will get the category 'Nacidos en 1981' (Born in 1981).

This page on the Spanish Wiki YouTube Pedia uses the template like this:. Can you guess what category gets added to the page? Nacidos en 1990, of course!

IFEQ
ifeq is an extension of if that is needed to check whether 2 values are the same or different, that is why it is called 'if equal' shortened to 'ifeq'. There are traditionally 4 parts that need to be mentioned for this function to work: Pay attention that this function is meant to work with strings, strings are words, so anything we put inside must be plain text. Using numbers is also possible; however, it is impossible to use anything functional there.
 * 2 values that we compare (they must be the same type, i.e. both words or both numbers),
 * what to do if they are equal,
 * what to do if they are not equal.

For example, we  CANNOT  use   because theme-fandomdesktop-light is a CSS class which means it is not plain text, and true will also be seen as a word, not as a boolean. So the expression above will make Fandom see both values as strings and check whether the word theme-fandomdesktop-light is the same as the word true (which it obviously is not), their functionality will be lost, and the desirable effect will not be achieved.

What we  CAN  use is the following: In this case we would be asking Fandom to check our infobox (or any other template) and see the number we put for the season of the anime we are talking about on the wiki. Imagine we have 3 seasons: 1 (10 episodes), 2 (12 episodes) and 3 (12 episodes). So if I say |season=1, I automatically see 'Number of episodes: 10' on my wiki page. If I use |season=2 or |season=3, Fandom knows that there needs to be 'Number of episodes: 12'. Season  < /data> Number of episodes     < /data>

SWITCH
Here comes the part that most of us love: switch. What can possibly be better than just letting the computer decide automatically what to do with the information we put in? No headache for us, we write the code once, and then Fandom is forever aware of what to do in difficult situations.

As HammerOfThor once said, "We saw above that ifeq: can be used to check if our parameter matches a given value, and then do different things if this is true or false. Suppose that instead of checking against one value, we want to check against multiple values and then do different things for each one. While we could do this with multiple ifeq: loops all nested in each other, this would be very complicated and confusing. A simpler solution is to use switch." ― HammerOfThor, 9000 BC

To put it simply, switch compares one word against several test cases and returns something that matches. It basically says 'here are 10 words, if you find 'Meow' among them, please replace it with a cat picture. If no, well, sad life then, no cat pictures on this page'.

Switch parsers are best displayed with each option on a new line. The system starts checking for a match from top to bottom, and as soon as one match is found, the code stops working. Therefore if there are a few matching cases, the first one will be chosen.

Looking at the code above, we can see an obvious repetition. However, our cat page will still get the necessary cat picture, because after the first match has been found, Fandom does not check what else matches our meow in the algorithm.

Default
If Fandom has looked through our switch but has not found any matches, it will not show anything. However, we can set a default value that would be given in this case. It is written on the last place without the equals sign = and has no replacement values.

A good simple example exists on Avatar Wiki where switch is used to determine the page name and then use it in a character infobox. This template here says 'If you see this page name, please give me a specific era when this character was born'.

For example, they have  | Aang = 12 BG, and we see it in their infobox here:

Grouped
There is an option to use one replacement for many cases that are grouped. Let us replace the second meow with something that will actually work and that has variations:

SECTION
The last but not the least is this tiny parser that can sometimes save a lot of re-writing: #section. Quite honestly, it works exactly like a template without creating extra pages, and the functionality is limited to 'post the same text on another page'.

A real life example on Disney Speedstorm Wiki would be Skill descriptions. Each character has a set of skills, and those skills have their general descriptions + level-by-level descriptions. Do I really want to copypaste those each time for each character? And what if something changes, will I have to go and edit every page that has this skill description mentioned? No, I play a smartie card and use sections instead!

This page has all descriptions for all skills. Each description is wrapped into  . For example:     

Then I go to any character page that needs this Boost description and do the following: 'Hello, Fandom, could you go to page Skills and find a section called Boost? Please paste it here.' Job done!

IF + SWITCH
We can always combine the desirable functions. For instance, there is an infobox on Disney Speedstorm Wiki that is used for Skill pages. Some skills can only be thrown at opponents in front of us, some skills can be thrown both in front and behind. And in case a skill can be used backwards, we would like to turn it into a link that sends us to a buttons page to show what button is pressed to use the skill backwards. Here are the examples of both cases: What do we do in this situation? Do we create 2 templates and call them Template:BackwardsNO and Template:BackwardsYES ? Here comes the if life-saver! Our logic in this case would be the following:

* For smartie-pants out there: it works exactly like the if/else statement in JS or any other language, where we have a condition, actions when it is true and actions when it is false.


 * 1) First of all, let us see what happens when we turn the word 'yes' into a link. This is the code from the template page:  {{ #switch:  {{{  backwards}}}  | Yes =  Yes   What we did there is very simple: ' {{#:  Hello, Fandom, if you see me use the word 'backwards' on a page ( |backwards=something ), please check what exactly I am writing there. | If it is a 'yes' ( |backwards=yes ), = please switch it with the following link  Yes  '.
 * 2) The code above is something that goes into our initial logic instead of ' turn yes into a link '. Say it is a no can just be replaced with a simple 'No' that we see on the final page. Therefore our | turn yes into a link | say it is a no  turns into  {{ #switch:  {{{  backwards}}}  | Yes =  Yes  | No }}
 * 3) Now all is left to do is to wrap this into our initial  {{ #if: }} format.  {{ #if:  {{{  backwards|}}}  === Yes |  {{ #switch:  {{{  backwards}}}  | Yes =  Yes  | No }} }}

{{clear}} I hope you have enjoyed the guide and found it helpful!

{{Navigation/Advice Index/Mentor Steps}}