Conditional Statements#
Conditional statements are a fundamental component of the Miva Merchant Template Language, enabling developers to control the flow of logic in their templates. By using conditional statements, you can dynamically render content, implement decision-making logic, and create personalized user experiences based on various conditions.
This section covers the syntax and usage of the three primary conditional statements in Miva: <mvt:if>
, <mvt:else>
, and <mvt:elseif>
.
<mvt:if>
: Basic Conditional Logic#
The <mvt:if>
tag is used to execute a block of code if a specified condition evaluates to true
. If the condition is not met, the code inside the <mvt:if>
block is skipped.
Syntax:#
<mvt:if expr="condition">
<!-- Code to execute if the condition is true -->
</mvt:if>
Example: Checking the Current Screen#
<mvt:if expr="g.Screen EQ 'SFNT'">
<p>Welcome to our storefront!</p>
</mvt:if>
In this example, the condition checks if the global variable g.Screen
equals 'SFNT'
(the storefront screen). If true, the message “Welcome to our storefront!” is displayed.
Key Components of a Condition:#
- Variable: The data being evaluated (e.g.,
g.Screen
). - Comparison Operator: Determines how values are compared (e.g.,
EQ
,NE
,GT
). - Value: The value being compared against (e.g.,
'SFNT'
).
<mvt:else>
: Providing an Alternative#
The <mvt:else>
tag is used to execute code if the condition in the <mvt:if>
block is not met. This allows you to handle alternative scenarios.
Syntax:#
<mvt:if expr="condition">
<!-- Code to execute if the condition is true -->
<mvt:else>
<!-- Code to execute if the condition is false -->
</mvt:if>
Example: Displaying Alternative Content#
<mvt:if expr="g.Screen EQ 'SFNT'">
<p>Welcome to our storefront!</p>
<mvt:else>
<p>Check out our latest deals on <a href="/products">products</a>!</p>
</mvt:if>
If g.Screen
equals 'SFNT'
, the storefront message is displayed. Otherwise, the alternative message encouraging users to view products is shown.
<mvt:elseif>
: Evaluating Multiple Conditions#
The <mvt:elseif>
tag allows you to check additional conditions within the same conditional block. It eliminates the need to write separate <mvt:if>
statements, streamlining your logic.
Syntax:#
<mvt:if expr="condition1">
<!-- Code to execute if condition1 is true -->
<mvt:elseif expr="condition2">
<!-- Code to execute if condition2 is true -->
<mvt:else>
<!-- Code to execute if neither condition is true -->
</mvt:if>
Example: Handling Multiple Screens#
<mvt:if expr="g.Screen EQ 'SFNT'">
<p>Welcome to our storefront!</p>
<mvt:elseif expr="g.Screen EQ 'PROD'">
<p>Buy now and get free shipping on orders over $50!</p>
<mvt:else>
<p>Explore our categories for the best deals!</p>
</mvt:if>
In this example:
- If the screen is
'SFNT'
, a welcome message is displayed. - If the screen is
'PROD'
, a promotion message is displayed. - If neither condition is met, a general call-to-action message is shown.
Best Practices for Conditional Statements#
-
Simplify Logic:
- Avoid overly complex expressions in a single
<mvt:if>
or<mvt:elseif>
statement. - Break down large conditions into smaller, readable parts.
- Avoid overly complex expressions in a single
-
Use Logical Operators:
- Combine conditions using
AND
orOR
to handle multiple scenarios in one statement. - Example:
<mvt:if expr="g.Screen EQ 'SFNT' AND g.User_Type EQ 'admin'"> <p>Welcome, Admin, to the storefront!</p> </mvt:if>
- Combine conditions using
-
Provide Defaults:
- Always include an
<mvt:else>
block to handle unexpected scenarios or provide fallback content.
- Always include an
-
Comment Your Code:
- Document complex conditions to make your logic easier to understand and maintain.
Advanced Example: Nested Conditions#
For more complex scenarios, you can nest conditional statements to evaluate multiple layers of logic.
<mvt:if expr="g.User_Logged_In EQ 'yes'">
<mvt:if expr="g.Screen EQ 'SFNT'">
<p>Welcome back to our storefront!</p>
<mvt:else>
<p>Enjoy exploring our site!</p>
</mvt:if>
<mvt:else>
<p>Please <a href="/login">log in</a> to see personalized content.</p>
</mvt:if>
In this example:
- If the user is logged in and on the storefront screen, a personalized message is displayed.
- If logged in but not on the storefront screen, a general message is shown.
- If not logged in, the user is prompted to log in.
Summary#
Conditional statements like <mvt:if>
, <mvt:else>
, and <mvt:elseif>
are powerful tools for building dynamic and engaging Miva Merchant templates. By mastering these tags and adhering to best practices, you can create flexible templates that adapt to a wide range of scenarios.