<mvt:eval>
#
The <mvt:eval>
tag in Miva Merchant’s template language is used to execute an expression and output its value directly to the page. It functions similarly to <mvt:assign>
but does not store the value in a variable—instead, it writes the result immediately to the template’s output.
This makes <mvt:eval>
a simple and efficient way to display calculated or dynamic content directly within your templates.
Empresa Requirement
Requires Miva Empresa Engine 5.18 or higher.
Syntax#
<mvt:eval expr="expression, string, or number" />
Attributes#
Attribute | Description |
---|---|
expr |
The expression to evaluate, which can include strings, numbers, variables, or functions. |
Examples#
Basic Arithmetic#
Perform arithmetic operations directly in the expression.
The sum is: <mvt:eval expr="1 + 2" />
Output:
The sum is: 3
Outputting Strings#
Display a string value directly on the page.
<mvt:eval expr="'Hello, Miva!'" />
Output:
Hello, Miva!
Using Numbers#
Perform calculations with numeric values.
<mvt:eval expr="(10 + 5) * 2" />
Output:
30
Using Expressions#
Execute complex expressions or functions supported by the Miva Empresa engine.
<mvt:assign name="l.myvariable" value="'Hello World'" />
<mvt:eval expr="substring(l.myvariable, 2, len(l.myvariable))" />
Output:
ello World
Concatenation#
Combine strings and calculated values using the $
concatenation operator.
<mvt:eval expr="'The length of this string is: ' $ len('example text')" />
Output:
The length of this string is: 12
Best Practices#
-
Keep Expressions Simple:
- Avoid overly complex expressions in
<mvt:eval>
. Use separate<mvt:assign>
statements for clarity when needed.
- Avoid overly complex expressions in
-
Combine with Functions:
- Use Miva functions like
substring
,len
, or mathematical operations for dynamic content generation.
- Use Miva functions like
-
Use Concatenation Wisely:
- The
$
operator is powerful but can become difficult to read in complex expressions. Break down concatenations if necessary.
- The
-
Comment Your Code:
- For advanced expressions, include comments to explain their purpose for better maintainability.
Advanced Example: Dynamic Output with Calculations#
This example combines multiple operations to display dynamic content.
<mvt:assign name="g.base_price" value="50" />
<mvt:assign name="g.discount" value="10" />
<mvt:assign name="g.final_price" value="g.base_price - g.discount" />
<p>Base Price: $<mvt:eval expr="g.base_price" /></p>
<p>Discount: $<mvt:eval expr="g.discount" /></p>
<p>Final Price: $<mvt:eval expr="g.final_price" /></p>
Output:
Base Price: $50
Discount: $10
Final Price: $40
Summary#
The <mvt:eval>
tag is a versatile tool for directly outputting calculated or dynamic content in Miva templates. By combining strings, numbers, variables, and functions, <mvt:eval>
allows you to create highly dynamic and interactive e-commerce pages.
Key Points:#
- Use
<mvt:eval>
to directly output the result of expressions. - Combine with Miva functions and operators for powerful dynamic content generation.
- Avoid excessive complexity in single expressions for better readability and maintainability.