Expressions#
Expressions can contain formulas, variables, literal values (text or numbers), functions, and operators, which indicate how the other components in the expression are to be evaluated. Expressions always occur in attribute values and must be enclosed in double quotes and curly braces, "{expression}"
.
Spaces between the quote and braces are not allowed (e.g. wrong " { expression }"
). Nearly every attribute can be evaluated as an expression. Some functions however expect specific keywords: in <MvFUNCTION>
, the name attribute must be a string literal and conform to function naming conventions.
Literal strings in expressions are enclosed in single quotes (e.g. "{ 'MivaMerchant' }"
), and strings are concatenated using the dollar sign operator ($
).
Syntax#
"99.95"
"{ 'Miva ' $ 'Script' }"
"{ 10 + 20 }"
Expressions are evaluated and return a result that can be assigned to a variable or output to the browser. Results can return a logical condition as false
(0
or null
) or true
(everything else, often represented by 1
).
You can then take action based on the results. Spaces in an expression are not evaluated unless they’re part of the data (e.g. within a string). Spaces inside literal strings are evaluated as part of the string.
Examples#
"{ g.price - g.discount }"
"{ address $ ' ' $ city $ ', ' $ state }"
"{ toupper(g.name) }"
"{ l.product:cost ROUND 2 }"
"{ 'Ms. ' $ trim(g.name) }"
If an operator contains characters that can occur in a variable name, then it must be separated by spaces. For example, { n POW 2 }
cannot be written as {nPOW2}
, but {a + b}
and {a+b}
are equivalent (since +
cannot be part of a variable name).
Data Types#
MivaScript does not require variables to be declared or initialized before use; they are type-less. The same variable can contain numbers, text, dates, or boolean values. In MivaScript, a zero or null value is considered false
; all other values are considered true
.
<MvASSIGN NAME="g.birthday" VALUE=338961600> Unix style date number.
<MvASSIGN NAME="g.birthday" VALUE="09/28/1980"> String
<MvASSIGN NAME="g.birthday" VALUE="September 28, 1980"> String
<MvASSIGN NAME="g.birthday" VALUE="{ s.dyn_time_t EQ 338961600 }"> This expression is false
<MvIF EXPR="{ g.birthday }">
Evaluates as true for the first three g.birthday and false for the last one.
</MvIF>
It is up to you to enforce data typing in your expressions. Type-less variables can lead to bugs if mixed incorrectly, but they also allow flexibility. Both examples below generate compiler errors due to mixing string and numeric types; however, the final example works, producing a string even when variables are numeric (though the result may not be what you expect):
<MvASSIGN NAME="g.birthday" VALUE="{ 'September, ' $ 28 $ ', ' $ 1980 }"> Operator type does not match constant type; inefficiency or unexpected rounding may occur.
<MvASSIGN NAME="g.birthday" VALUE="{ 'September, ' + 28 + ', ' + 1980 }"> Operator type does not match constant type; inefficiency or unexpected rounding may occur.
<MvASSIGN NAME="g.birthday" VALUE="{ 'September, ' $ '28, ' $ '1980' }"> This works because everything is a string.
<MvASSIGN NAME="g.birthday" VALUE="{ g.month $ ' ' $ g.day $ ', ' $ g.year }"> This works even if ALL the variables are numbers, but the result "9 28, 1980" is probably not what you wanted.
One possible strategy for avoiding these kinds of issues is to declare string types in the variable name itself (e.g. g.str_birthday
).
Using Expressions#
Expressions are most commonly assigned or evaluated within <MvIF>
for conditionals, but they can also be used directly inside HTML tag attributes. In the example below, an email is sent only if the form was submitted; this demonstrates four methods of using expressions. See Operators for more.
<MvASSIGN NAME="g.email:to" VALUE="sales@mydomain.com">
<MvIF EXPR="{ (g.action EQ 'send_email') }">
<MvIF EXPR="{ Send_My_Email(g.email) }">
<MvEVAL EXPR="{ 'Your message was sent.' }">
<MvELSE>
<MvEVAL EXPR="{ 'Your message could not be sent.' }">
</MvIF>
</MvIF>
<form>
<input type="hidden" name="action" value="send_email">
FROM: <input type="text" name="email:from" value="{ g.email:from }">
Message: <input type="text" name="email:message" value="{ g.email:email }">
... other fields ...
</form>
Special Characters#
Since expressions are surrounded by double quotes, you need special techniques to include double quotes inside literal strings (and for all non-printable characters below ASCII 32 or above 127). You can escape characters with a backslash or use the asciichar()
function. See the ASCII character list for reference.
<MvEVAL EXPR="{ 'I can't believe you are \"30\" years old.' }">
<MvASSIGN NAME="tab" VALUE="{ asciichar(9) }"> tab
<MvASSIGN NAME="lf" VALUE="{ asciichar(10) }"> line feed
<MvASSIGN NAME="dq" VALUE="{ asciichar(34) }"> double quote
<MvASSIGN NAME="cr" VALUE="{ asciichar(13) }"> carriage return
<MvASSIGN NAME="crlf" VALUE="{ asciichar(13)$asciichar(10) }"> CR+LF
<MvEVAL EXPR="{ 'Dear User,' $ crlf $ 'Thank you for your interest.' }">