Magic words

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, {{#if:}}), 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 **{{** ?). Also please make sure you know it is a special magic tag because of the **#** I used. Then remember what **if** is for, and then listen to my conditions, so here I start **:**'.

Each new bit of information is added with a single vertical line **|** before the closing curly brackets **}}**.

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 {{#if:  {{{data|}}} | There is something | There is nothing}}. 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'.

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 **{{{data|}}}** 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!'

 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 {{{data|}}} 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):

More rows neededFewer rows neededFewer rows but without the IF condition in style
## 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.

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:

{{#ifexpr: {{{3}}}<1981 | [[Categoría:Nacidos antes de 1980]] | [[Categoría:Nacidos en {{{3}}}]]}}

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) +  {{{3}}} 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: {{Nacimiento|13|02|1990}}. 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:

* **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.

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.

For example, we **CANNOT** use
{{#ifeq: theme-fandomdesktop-light | true  | apply black text color  | apply white text color }}
 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'.

<data source="season">
<label>Season</label>
</data>

<data source="episodes">
<label>Number of episodes</label>
</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.

{{#switch:  {{{sound|}}}

}}

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.

{{#switch:  {{{sound|}}}

}}

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:

{{#switch:  {{{sound|}}}

}}

## 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 again. 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 <section begin=SkillName/> <section end="SkillName" />. For example:

<section begin="Boost" />

{{Skill
}}<section end="Boost" />

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.'
 {{#section:Skills|Boost}}

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:

{{#if: backwards is *yes* | turn *yes* into a link  | say it is a *no* }}

*\* 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 = [[Buttons#Backwards|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
 turns into
 {{#switch: {{{backwards}}}  | Yes = [[Buttons#Backwards|Yes]] | No}}
3. Now all is left to do is to wrap this into our initial **{{#if: }}** format.

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