Skip to content

Understanding MivaScript Syntax and Functions#

MivaScript is a tag‑based templating language designed to embed dynamic logic directly within Miva Merchant pages. Its syntax revolves around XML‑style tags, expression delimiters, and scoped variables. This overview introduces the core elements you’ll encounter when writing MivaScript, then dives deeper into how custom merchant functions fit within that syntax.

Core Syntax Elements#

Tags#

All dynamic behavior in MivaScript is implemented through tags prefixed with Mv. Tags follow an XML‑like structure:

<MvTAGNAME ATTRIBUTE1="value" ATTRIBUTE2="value">
  ...inner content or nested tags...
</MvTAGNAME>

Self‑closing tags omit the closing tag:

<MvASSIGN NAME="l.count" VALUE="{1}">

Block tags enclose nested logic or markup, such as <MvFUNCTION>…</MvFUNCTION> or <MvIF>…</MvIF>.

Expression Delimiters#

Whenever you need to compute values or embed variables, enclose your expression in curly braces {}. Inside these braces, you can reference variables, perform arithmetic, call functions, and concatenate strings:

{ l.price * l.quantity }              <!-- arithmetic -->
{ 'Hello, ' $ g.userName $ '!' }      <!-- string concatenation -->
{ FormatCurrency(l.total) }           <!-- function call -->

The $ operator concatenates text segments; functions and arithmetic operate as expected.

Comments#

Use the <MvCOMMENT> tag to include developer notes or disable sections without affecting output:

<MvCOMMENT>
  This section calculates the user’s discount
</MvCOMMENT>

Variables and Scope#

MivaScript distinguishes variables by scope using prefixes:

Prefix Scope Lifetime
l. Local Current function or template render
g. Global Entire page request
s. Session User session across pages
m. Merchant Store‑wide settings (read‑only)

Declare or modify variables with <MvASSIGN>:

<MvASSIGN NAME="l.discount" VALUE="{ l.price * 0.1 }">

This stores the computed value in l.discount without producing output.

Control Structures#

Conditional Logic#

Use <MvIF>, <MvELSEIF>, and <MvELSE> to branch based on expressions:

<MvIF EXPR="{ l.stock > 0 }">
  <p>In stock</p>
<MvELSE>
  <p>Out of stock</p>
</MvIF>

Loops#

The <MvWHILE> tag repeats its contents while a condition holds true:

<MvWHILE EXPR="{ l.index < 5 }">
  <p>Item { l.index }</p>
  <MvASSIGN NAME="l.index" VALUE="{ l.index + 1 }">
</MvWHILE>

Defining and Using Merchant Functions#

Merchant functions let you package reusable logic into named routines. They follow the same tag syntax but introduce their own attributes.

Declaring a Function#

<MvFUNCTION NAME="MyFunction" PARAMETERS="var.input1, input2" STANDARDOUTPUTLEVEL="">
  ...function body...
</MvFUNCTION>
  • NAME sets the function’s identifier.
  • PARAMETERS lists comma‑separated inputs. Prefix with var to pass by reference. Inside the body, each parameter is available as l.parameterName.
  • STANDARDOUTPUTLEVEL (optional) controls output visibility based on debug settings.

Returning Values#

Inside a function, compute intermediate values with <MvASSIGN>, then use <MvFUNCTIONRETURN> to send back your result:

<MvFUNCTION NAME="AddNumbers" PARAMETERS="a, b">
  <MvASSIGN NAME="l.sum" VALUE="{ l.a + l.b }">
  <MvFUNCTIONRETURN VALUE="{ l.sum }">
</MvFUNCTION>

Invoking Functions#

Once defined, call your function in several ways:

  • Inline output via <MvEVAL>:

<MvEVAL EXPR="{ MyFunction(5, 3) }">
* Conditional checks in <MvIF> or <MvWHILE>:

<MvIF EXPR="{ IsUserVIP(customerId) }"></MvIF>
* Variable assignment with <MvASSIGN>:

<MvASSIGN NAME="l.result" VALUE="{ ComputeOrderValue(var.cart, 0.08) }">

By mastering tags, expressions, variables, control structures, and functions, you can craft powerful, maintainable templates that adapt to your store’s unique needs. For deeper dives into variable assignment, expression evaluation, and error handling, explore the specific function documentation.