<mvt:assign>
#
The <mvt:assign>
tag is a fundamental component of the Miva Template Language, used to assign values to variables. It evaluates the expression provided in the value
attribute and stores the result in the variable defined by the name
attribute.
Key Features:#
- Supports global (
g.
) and local (l.
) variable assignment. - Allows assignment of strings, numbers, expressions, or combinations of these.
- Enables dynamic manipulation of data within templates.
Requires Miva Empresa Engine 5.18 or higher.
Syntax#
Pseudo Syntax:#
<mvt:assign name="variable" value="expression, string, or number" />
Basic Example:#
<mvt:assign name="g.sum" value="1 + 2" />
<p>The sum is: &mvt:global:sum;</p>
Output:
The sum is: 3
Attributes#
Attribute | Description |
---|---|
name |
Defines the variable name. Use g. for global variables or l. for local variables. |
value |
The value to assign. Can be an expression, string, number, or combination. Strings require single quotes. |
Notes and Best Practices#
- Global vs Local Scope:
- Global Variables (
g.
): Always accessible in templates using&mvt:global:VariableName;
. -
Local Variables (
l.
): Limited to the context where they are defined. Only variables in thel.settings
structure can be output directly. -
Strings: Enclose in single quotes. Unquoted text is treated as a variable.
- Numbers: Do not require quotes.
- Expressions: Automatically evaluated. Ensure proper syntax for functions and operations.
Examples#
Assigning Strings#
Assign a string to a variable.
<mvt:assign name="l.settings:foo" value="'bar'" />
<p>Value: &mvt:foo;</p>
Output:
Value: bar
Assigning Numbers#
Perform mathematical operations and assign the result to a variable.
<mvt:assign name="g.result" value="(10 + 20) * 3" />
<p>Result: &mvt:global:result;</p>
Output:
Result: 90
Using Expressions#
Evaluate an expression and assign the result.
<mvt:assign name="l.modified" value="substring('hello world', 2, len('hello world'))" />
<p>Substring: &mvt:modified;</p>
Output:
Substring: ello world
Concatenating Strings#
Use the $
operator to concatenate strings.
<mvt:assign name="g.message" value="'Hello, ' $ 'John!'" />
<p>Message: &mvt:global:message;</p>
Output:
Message: Hello, John!
Outputting Values#
To display a variable’s value on the page, the variable must either:
- Be a global variable.
- Be part of the
l.settings
structure.
Global Variable Example:#
<mvt:assign name="g.total" value="42" />
<p>Total: &mvt:global:total;</p>
Output:
Total: 42
Local Variable Example:#
<mvt:assign name="l.settings:discount" value="'15%'" />
<p>Discount: &mvt:discount;</p>
Output:
Discount: 15%
Incorrect Usage:#
Attempting to output a local variable not in the l.settings
structure.
<mvt:assign name="l.temp" value="'Temporary Value'" />
<p>Value: &mvt:temp;</p>
Output:
(No output; variable is not part of `l.settings`)
Advanced Example: Dynamic Calculation#
Combine numbers, strings, and functions dynamically.
<mvt:assign name="g.result" value="'The price after discount is: ' $ (100 - 25)" />
<p>&mvt:global:result;</p>
Output:
The price after discount is: 75
Summary#
The <mvt:assign>
tag is a versatile and essential tool for managing data within Miva templates. By understanding its capabilities and best practices, you can:
- Dynamically assign and manipulate variables.
- Implement conditional logic and data transformations.
- Enhance the interactivity and functionality of your templates.