Skip to content

Operators#


Operators in the Miva Template Language are essential for performing logical, comparison, string manipulation, and mathematical operations. They allow you to build dynamic and functional templates by enabling complex expressions, calculations, and data evaluations.

This guide explores the various operator types available in Miva, with practical examples to illustrate their usage.


Types of Operators#

Logical Operators#

Logical operators combine conditions to form expressions that determine the flow of your template logic. Common logical operators include AND, OR, NOT, and ISNULL.

Examples:#

Check for Null Values:

<mvt:assign name="g.color" value="''" />
<mvt:if expr="ISNULL(g.color)">
    <p>The variable is NULL.</p>
</mvt:if>

Combining Conditions:

<mvt:assign name="g.price" value="20" />
<mvt:assign name="g.stock" value="10" />
<mvt:if expr="(g.price GT 15) AND (g.stock GT 5)">
    <p>The product is available and meets the price threshold.</p>
</mvt:if>


Comparison Operators#

Comparison operators are used to evaluate the equality, inequality, or relative magnitude of values.

Operator Example Explanation
EQ g.screen EQ 'SFNT' Returns true if g.screen is equal to 'SFNT'.
NE g.screen NE 'SFNT' Returns true if g.screen is not equal to 'SFNT'.
GT l.settings:product:price GT 15 Returns true if product:price is greater than 15.
LT l.settings:product:price LT 15 Returns true if product:price is less than 15.
GE l.settings:product:price GE 15 Returns true if product:price is greater than or equal to 15.
LE l.settings:product:price LE 15 Returns true if product:price is less than or equal to 15.

Example:#

Price Comparison:

<mvt:if expr="l.settings:product:price GE 20">
    <p>The product qualifies for premium pricing.</p>
<mvt:else>
    <p>The product is below the premium price threshold.</p>
</mvt:if>


String Operators#

String operators help manipulate and evaluate strings within templates. Key operators include IN (substring matching), CIN (case-insensitive substring matching), and $ (concatenation).

Operator Example Explanation
IN 'abc' IN g.text Returns true if 'abc' is a substring of g.text.
CIN 'abc' CIN g.text Returns true if 'abc' is a case-insensitive substring of g.text.
$ 'Hello ' $ 'World' Concatenates two strings and returns 'Hello World'.

Examples:#

Substring Check:

<mvt:assign name="g.description" value="'This is a test description'" />
<mvt:if expr="'test' IN g.description">
    <p>The description contains the word "test".</p>
</mvt:if>

Concatenation:

<mvt:assign name="g.name" value="'John'" />
<mvt:assign name="g.greeting" value="'Hello ' $ g.name" />
<p>&mvt:greeting;</p> 

Output in HTML:

Hello  John


Mathematical Operators#

Mathematical operators allow for arithmetic operations such as addition, subtraction, multiplication, and division.

Operator Example Explanation
+ g.total + 5 Adds 5 to g.total.
- g.total - 5 Subtracts 5 from g.total.
* g.price * 2 Multiplies g.price by 2.
/ g.price / 2 Divides g.price by 2.

Example:#

Calculate Discount:

<mvt:assign name="g.price" value="100" />
<mvt:assign name="g.discount" value="10" />
<mvt:assign name="g.discounted_price" value="g.price - g.discount" />
<p>Discounted Price: &mvt:global:discounted_price;</p>

Output in HTML:

Discounted Price: 90


Best Practices#

  1. Combine Logical Operators Thoughtfully: Avoid overly complex expressions. Break down logic into smaller, manageable conditions.

  2. Test for Edge Cases: When comparing strings or numbers, test edge cases such as null values, empty strings, or large numbers.

  3. Optimize Arithmetic: Ensure arithmetic operations are necessary and avoid unnecessary calculations in loops for performance.

  4. Comment Your Logic: Document complex expressions with comments to ensure maintainability.