Skip to content

Conditional Statements#


Conditional statements, including if statements and if/else statements, constitute a fundamental aspect of the Miva Merchant template language, empowering developers with robust logical operations. These statements are pivotal in harnessing the full potential of Miva Merchant’s templating capabilities.

<mvt:if>#

The syntax for an if statement in Miva Merchant is standardized: mvt:if exp=”, where exp denotes an expression evaluated by the engine. Within this expression, conditions are specified, and if met, the corresponding code is executed.

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:

<mvt:if expr="condition">
    Code to execute if condition is true
</mvt:if>

Basic comparisons have three parts of a condition: the variable, a comparison operator, and a value.

A prevalent use case for if statements is determining the current page or screen within Miva Merchant. For instance, testing the value of g.Screen, which represents the current screen, involves checking if it equals a specific string, denoted by single quotes. In Miva Merchant, equality comparison is indicated by EQ.

To validate this, we can execute the code within Miva Merchant, placing it in any suitable area and updating the content. Upon refreshing the front end, the message “This is the storefront!” confirms the successful execution of the if statement.

A basic comparison looks like this:

<mvt:if expr="g.Screen EQ 'SFNT'">
    <p>Welcome to our awesome site!</p>
</mvt:if>

<mvt:else>#

Moving beyond basic if statements, Miva Merchant offers variations such as the if/else statement, which executes alternative code when the initial condition is not met. Additionally, the else:if statement provides a means to evaluate multiple conditions sequentially, executing corresponding code blocks based on their fulfillment. These else:if statements can be nested to accommodate complex logic structures, facilitating extensive conditional branching within templates.

The code for an if-else statement looks like this:

<mvt:if expr="g.Screen EQ 'SFNT'">
    <p>Welcome to our awesome site!</p>
<mvt:else>
    <p>Be sure to check out our sale on all <a href="path-to-shirts">men's shirts</a>!</p>
</mvt:if>

The above code is the exact same as our original if statement, with the addition of code to execute if the original condition is false. When on the Storefront page, the message “Welcome to our awesome site!” will appear, but if we are on ANY other page besides SFNT, the promotional text for the men’s shirt sale will appear.

<mvt:elseif>#

The <mvt:elseif> statement can accomplish the same goal as writing two separate if statements. Using the <mvt:elseif> statement is simple, you open an if statement, check if a condition is true, then rather than exiting the if statement, we can check another condition with <mvt:elseif>.

The code for an <mvt:elseif> statement looks like this:

<mvt:if expr="g.Screen EQ 'SFNT'">
    <p>Welcome to our awesome site!</p>
<mvt:elseif expr="g.Screen EQ 'PROD'">
    <p>Spend over $50 and get FREE shipping!</p>
</mvt:if>

The code above is checking for the same condition as our original if statement, is the current Screen the SFNT. Immediately following that statement, rather than close the if statement, <mvt:elseif> is added. This time, the conditions checks if the Screen is PROD, if it is, a message about free shipping is displayed. If neither of those conditions are met, the code exits the if statement and does not run any code at all.