Global & Local Variables#
Entities in Miva are parsed through the Empresa and are replaced with their actual value before they are sent to the browser.
Similar to other programming languages an entity is essentially a variable. The difference is entities will only print the value to the screen, they can’t be assigned or be used tp perform operations. To do those types of things you will need to access the variable using its scope identifier.
Miva offers two distinct types of variables: global and local. Each Miva Merchant Entity has a scope. It is either a local variable which is accessed using l.settings: or a global variable accessed using g.
Local Variables#
Conversely, local variables, denoted by the prefix l.
, are confined to a local scope, typically within a specific context or block of code. While local variables can’t be directly output to the page using the &mvt:
syntax, they’re essential for conditional statements. For instance, an “if statement” might compare the value of a local variable, such as l.name
, against a specific criterion. Local variables offer precision and encapsulation, ensuring that data is managed effectively within specific contexts.
l.settings:product:formatted_price
l.settings:store:name
l.settings:category:name
l.settings:product:thumbnail
The structure of a local variable starts with l.settings:
followed by the item being referenced product:
followed by the specific variable you are accessing formatted_price
Unlike printing an entity to the screen using &mvt:
, when you reference a entity using the scope identifiers, there is no semicolon at the end.
l.settings structure#
A notable exception to the rule regarding local variables’ output is the l.settings
structure. Although technically a local variable, l.settings
operates akin to a global variable, allowing for output directly to the page. Frequently utilized across Miva’s template language, l.settings
serves as a specialized repository for various page-related information. To output values stored within l.settings
, developers use the &mvt:
syntax followed by the variable name, such as &mvt:name;
.
Global Variables#
Global variables, as the name suggests, possess a global scope, meaning they’re accessible anywhere on the page. They are identified by the prefix g.
followed by the variable name. For instance, a global variable named g.name
would be declared with a value such as John
, and referenced using &mvt:global:name;
to output its value on the page. Global variables are versatile and can be employed for various purposes, from simple output to conditional statements.
g.Screen
g.Store_Code
g.Product_Code
The structure of a global variable is always g.
than the variable that you are accessing, Screen
. Unlike local variables, there is no item reference in the middle because it is a global variable.
The big question that this brings up is when to use a local variables l.settings:
and when to use global variables g.
. Luckily, this is pretty straight- forward. A variable is either one or the other. It will never be both. You can easily tell which to use by looking at how it is called in the page using &mvt:
.
If it contains an item reference in the middle like: &mvt:product:name;
you access it as a local variable like this: l.settings:product:name
If it looks like this: &mvt:global:Store_Code;
you access it as a global variable like this: g.Store_Code
.
In Summary#
While working with global and local variables in Miva Merchant, developers enjoy the flexibility of not needing to declare variable types explicitly. Variables can seamlessly adopt various data types, whether strings, integers, or expressions, without pre-declaration. This versatility simplifies variable management and enhances code readability.
Understanding the distinctions between global and local variables is fundamental for proficient development in Miva Merchant’s template language. By grasping the nuances and best practices associated with each variable type, developers can effectively harness the power of Miva’s templating capabilities.