Displaying Static Content is an essential part of creating websites. But what happens when we want to do more? What if we want to present specific data to a customer based on a choice they made or action they took? To do this, we need to be able to use conditional statements to let the browser know what content to display.
The most commonly used conditional statement is the if-statement. When the browser executes the statement, it checks to see if a condition is met. If this condition is met, it executes the code that it contains, if not, the code within the statement is skipped.
The code for an if statement looks like this:
Conditional statements like this help us to serve more specific content to the user based on past actions or choices they have made. For example, if a user is on a specific product, we might want to show a banner with a discount code for checkout.
Before I move on, I want to talk about the "condition" part of the above if statement. For basic comparisons, there are three parts of a condition: the variable, a comparison operator, and a value.
In the previous article, we learned what entities and variables are and how to output them on a page. In a conditional statement, we can access variables like g.Screen and l.settings:product:code, compare their value to another value, then run conditional code if the statement is true.
A basic comparison looks like this:
What we've done above is check to see if the value of g.Screen is equal to SFNT, if it is, then we write a welcoming paragraph tag for the user to see. You'll notice that instead of the word "EQUAL" or using an equal sign "=", I used just two letters EQ. You will want to remember to always do it this way, or else your statements will fail to execute.
The comparison operator EQ is just one of many in the Miva Merchant's templating language. There are many other ways to compare strings, numbers, and variables using other comparison operators, but you'll quickly realize there are a select few that always come into play. Take a quick look at the full list of comparison operators below.