24/7 Support: 800.608.6482

Get Started

Videos | Logic Operators

ver. 9.0 and later

Video Transcript

There are a few different groups of operators, which I wanted to dig into today. The first one is Logic Operators, then there's Comparison Operators, String Operators and Math Operators. So Logic Operators are things like AND, OR, NOT AND ISNULL. If you're familiar with any other program language I'm sure you've used these quite a bit. You may be familiar with AND written as two &&. That's how you do it in php and a few other languages, but in Miva, it's just the word "AND." By convention, it's written in all uppercase. This is mostly for code readability, however, it does work in lowercase versions. The same thing goes for OR. So OR, you may be familiar with the double pipe ||. With Miva it's just the word OR. NOT, this is similar to the exclamation point!. Finally, ISNULL is just written as ISNULL all in one word. While these are different that other programming languages, they are actually very easy to remember because it's just the word. Let's take a look at an example. So here I'm using mvt:assign to create a variable called g.color and then I'm initializing it with a value of an empty string with two single quotes. You think you would be able to initialize it with just nothing, however, that doesn't compile correctly within the template language. So when you want to create a NULL variable or an empty variable, you give it an empty string just like that. So here we have an if statement that says if ISNULL g.color then output the text "The variable is NULL." I'll go over to our Miva Merchant Admin so we go and run this. I'll go to the Storefront page and then the Content section and paste this in here. Now when I refresh I see the text the variable is NULL. Perfect, it's exactly what we expected.

You're not limited to a single logic operator or expression. So here, I can combine this and say "NOT ISNULL g.color" and let's give color a value of red so that returns true and then I can use "AND ISNULL" and here I can put any variable I want that doesn't exist because it will always return NULL. I'll put g.test and test doesn't exist I don't have it declared. So this expression says if g.color IS NOT NULL which it's not because it has a value and g.test ISNULL, so this actually should return true. So let's change this text to say "The expression is true." Let's go run this and make sure we did everything correctly. Once the text is in, we've hit update and then refreshed on the storefront page, you'll see that the expression is true. So that's Logic Operators, a very powerful part of the Miva Template Language.

Let's dig into Comparison Operators. So Comparison Operators are Equals, Not Equals, Greater Than, Less Than, Greater Than or Equal To and Less Than or Equal To. This is when you want to compare if this variable or this string is equal to something else. Now, you may be familiar with the double equal sign or the triple equal sign to test if two variables are equal to each other. Miva Merchant has a two-digit acronym for EQ. This does take a little bit of getting used to. However, pretty soon it will become second nature. Let's look at a few examples of these. Here I have two variables declared. g.test which has a numeric value of 10 and g.number which has a value of 100. So here in the expression we're saying if g.test is equal to g.number output "The numbers are equal" else output "The numbers are not Equal." So we should expect "The numbers are not Equal." Let's run this and make sure we get the right results and here we got "The numbers are not equal." Perfect, it's exactly what we'd expect. So let's try one more. If g.test is LE to g.number this should return true. Let's change our message to "g.test is less than g.number" and we get the right result.

The next group of operators are String Operators and Concatenation. Here there are three main operators that you want to be aware of. There's actually more than this, but these are the ones you'll use on a day-to-day basis. So the first one is IN. This is a String Comparison Operator and it says if this string is IN this other string. The next one is CIN, which is Case Insensitive Comparison. So if this string (ignoring case) is contained within this string. The third one is Concatenation. So the $ is Concatenation. It will concatenate two strings together and output the result. So let's take a look as some examples of these. So here I have a variable of g.name and the value is a string wrapped in single quotes of my name. I'm going to test the expression of if brennan (all lowercase) is IN g.name. Because IN is a case sensitive comparison this should actually not return anything. It shouldn't find a match because Brennan is lowercase here and it's uppercase here. So let's run this. We should see a blank page when we refresh this, which I do. Now let's go make a change to this. Instead of IN this time let's use CIN, which is a case insensitive comparison. This should return true. Perfect, exactly what we'd expect. As a rule of thumb, most the time when you're doing string comparisons, you'll probably use CIN. This will give you a little more flexibility and prevent you from making small errors when the strings actually are equals, just not case sensitive.

Let's now look at an example with the Concatenation Operator or the dollar sign. This is a very powerful operator and you'll use it quite a bit. So here I'm going to say, if the string Brenan Concatenated (so $) with my last name, heyde is contained in CIN since these are lowercase and this is uppercase here, contained in g.name and this should return true. You notice I added a space here to match the space that we have defined up here. I refreshed this and again, it found a match. That Concatenated the two strings together for brennan and heyde and then it did a string comparison to see if brennan heyde is contained within g.name which contains Brennan Heyde. It's a very useful and powerful operator.

The last set of Operators I want to look at are Math Operators. Again, there's more than just these four. However, these are the most common ones you'll use on a day-to-day basis. Here it's the same operators you're used to working with. Plus sign +, Minus sign -, Multiplication sign * and the Division sign /. These operators can be used within an if statement, as well as within mvt:eval or mvt:assign. Let's take a look at one example. So here I have an example called g.result and I'm doing some math operation. Again, you can combine them and group them with parentheses as you would with any other programming language. So here I'm taking 100 + 50 which is 150 and I'm diving it by 3 which will give me 50 and then I'm subtracting 25. The result here should be 25. If we run this it outputs 25 like we'd expect. So the Math Operators are very familiar and give you the tools you need to add Math logic to your page templates.

Example Code

Logic Operator

Comparison Operators

String Operators


Math Operators

comments powered by Disqus

Looking for Developer Docs?

We have a whole section for that, including: Developer Training Series, Template Language docs, Module Development tutorials and much, much more.

Head to the Developer Section

Miva believes that all online businesses should have access to a scalable ecommerce platform that can meet their unique business requirements. Miva offers PCI compliant ecommerce, hosting, and custom website design and development solutions. Miva customers have processed over $100 billion in online sales since 1997.

Copyright © 2016 Miva, Inc - All Rights Reserved   Privacy Policy | Store Policy

Contact Us
Receive Tips & Updates

Copyright © 2017 Miva, Inc - All Rights Reserved

Back To The Top