Miva Merchant Quick Tip: Matching Multiple Variables in a Conditional

This morning I was working on a small project to modify the layout of the attributes on a product page template, and it occurred to me that others might find this little trick about writing conditionals where you need to match a multitude of variables useful.

In this particular project, I needed conditionals for two purposes: one in one location of the template to check to see if the attribute template code was either the word “Length” or the word “Width” and the second in a separate location to check to see if the template code was either “LengthFraction” or “WidthFraction.”

Since I was trying to match the code against a list of variables, I could have written my conditional like so:

<mvt:if expr="l.settings:attribute:template_code IN 'LengthFraction, WidthFraction'">

Anyone else see a problem with this though? The above conditional, which checks to see if the content of the template code variable exists in either the words LengthFraction or WidthFraction will return true not only if the template code is actually LengthFraction or WidthFraction, but also if the template code is merely Length or Width.

So, we need to be a little more specific. Since I was only trying to match one of two different variables, I could have written my conditional like this instead:

<mvt:if expr="((l.settings:attribute:template_code EQ 'LengthFraction') OR (l.settings:attribute:template_code EQ 'WidthFraction'))">

That’s fine when you only have a couple of variables to test against, but if you have several codes you want to match up, there’s a much cleaner and easier way. By making use of Store Morph Technology’s (SMT) concatenating feature, you can use the IN expression to check against a list of several codes, without the risk of inexact matches.

Here’s the code:

<mvt:if expr="'|'$l.settings:attribute:template:code$'|' IN '|Length|Width|'">

And here’s how it works.

The very first element in the conditional is a string (enclosed by single quotes) consisting of a pipe character, aka | . Next to the string is a $ sign. In many scripting languages the $ sign is used to indicate a variable, but in SMT, the $ sign is a concatenator. That is, it takes whatever string or variable is in front of it, and joins it to whatever string or variable is after it.

The next element in our conditional is the SMT variable we are wanting to test, in this case, the attribute template code. It’s followed by another $ sign and another string consisting of a pipe character. The result is that the first part of our conditional expression consists of the template code surrounded by pipes:
|Length|
|LengthFraction|
|Width|
|WidthFraction| etc.

The second element in the conditional is a string, consisting of a list of the codes we want to match. The list begins and ends with a pipe character, and the codes in the list are also separated with pipe characters.

What this means is that if the conditional tries to match |Length| to |LengthFraction| it will now return false, since there isn’t an exact character to character match — although the |Length part of the first element is found in |LengthFraction| the pipe at the end of |Length| does not appear in that same position in |LengthFraction| and so the conditional will return false.

So there you have it. A complex conditional made short and sweet.

Be Sociable, Share!

Comments are closed.