Global & Local Variables#
In Miva Merchant, variables play a critical role in managing and displaying dynamic data. These variables, also referred to as entities, are parsed through the Empresa engine and replaced with their actual values before being sent to the browser.
Similar to other programming languages, variables in Miva serve as placeholders for data. However, unlike entities that only output values, variables allow for complex operations such as assignments and evaluations. This distinction is essential when working with global and local variables.
Global Variables#
Global variables, identified by the prefix g.
, are accessible throughout the entire template, regardless of context. These variables are versatile and suitable for storing data that needs to be reused or accessed globally.
Key Features#
- Scope: Accessible anywhere in the template.
- Syntax:
g.
followed by the variable name (e.g.,g.Store_Code
). - Output: Use
&mvt:global:VariableName;
to display the value.
Examples#
Declaring and Using Global Variables:
<mvt:assign name="g.Store_Code" value="'MIVA123'" />
<p>Store Code: &mvt:global:Store_Code;</p>
Output in HTML:
Store Code: MIVA123
Using Global Variables in Conditional Statements:
<mvt:assign name="g.Screen" value="'SFNT'" />
<mvt:if expr="g.Screen EQ 'SFNT'">
<p>This is the storefront screen.</p>
</mvt:if>
Common Global Variables#
g.Screen
: Represents the current screen being displayed.g.Store_Code
: Contains the store’s unique code.g.Product_Code
: Holds the product code of the current product.
Local Variables#
Local variables, denoted by the prefix l.
, are restricted to specific contexts or blocks of code. While they cannot be directly output to the page using &mvt:
, they are invaluable for operations like conditional checks and iterative loops.
Key Features#
- Scope: Limited to the specific context in which they are defined.
- Syntax:
l.settings:
followed by the item reference and variable name (e.g.,l.settings:product:name
). - Output: Typically used internally for logic, but
l.settings
variables can be output using&mvt:
syntax.
Examples#
Using Local Variables in Conditional Statements:
<mvt:assign name="l.settings:product:price" value="20" />
<mvt:if expr="l.settings:product:price GT 15">
<p>This product is premium priced.</p>
</mvt:if>
Accessing l.settings
Variables:
<mvt:assign name="l.settings:store:name" value="'My Store'" />
<p>Store Name: &mvt:store:name;</p>
Output in HTML:
Store Name: My Store
Common Local Variables in l.settings
#
l.settings:product:formatted_price
: Displays the formatted product price.l.settings:store:name
: Outputs the store name.l.settings:category:name
: Prints the name of the current category.
Comparing Global and Local Variables#
Feature | Global Variables (g. ) |
Local Variables (l.settings: ) |
---|---|---|
Scope | Accessible throughout the page | Limited to specific contexts |
Output | Use &mvt:global:VariableName; |
Use l.settings: or &mvt: for l.settings variables |
Typical Use | Data shared across templates | Context-specific operations |
Structure | g.VariableName |
l.settings:Item:VariableName |
When to Use#
- Global Variables: For data required across multiple sections of a template or logic.
- Local Variables: For data relevant only within specific loops or conditional blocks.
Best Practices#
-
Understand Scope:
- Use global variables for data that must persist across the template.
- Use local variables for temporary or context-specific data.
-
Avoid Overwriting:
- Name variables clearly to prevent accidental overwrites, especially when using global variables.
-
Leverage
l.settings
:- Take advantage of the
l.settings
structure to access and manage page-specific data efficiently.
- Take advantage of the
-
Optimize Conditional Logic:
- Use variables within conditional statements to improve the readability and maintainability of your templates.