|
ExpressionsExpressions 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 are expecting specific keywords. In <MvFUNCTION>, the name attribute must be a string literal and conform to function naming conventions. Literal strings in expression are enclosed in single quotes. e.g. "{ 'MivaMerchant' }". Literals don't have to be enclosed in curly brackets. Strings are concatenated using the dollar sign operator ( e.g. $ ). SyntaxExamples:Literal string : "Miva Script" Literal number : "99.95" String Expression: "{ 'Miva ' $ 'Script' }" Numeric Expression: "{ 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 logic condition as false (0 or null) or true (everything else, often represented by a 1). You can then take action based on the results. Spaces in an expression are not evaluated unless the spaces are an intrinsic part of the data being evaluated (e.g. a string). Spaces are simply separators between parts of the expression. Spaces inside literal strings are evaluated as part of the string. Examples:Numeric variables: "{ g.price - g.discount }" String variables: "{ address $ ' ' $ city $ ', ' $ state }" Miva Script functions: "{ toupper(g.name) }" Miva Script operators: "{ l.product:cost ROUND 2 }" Combined: "{ 'Ms. ' $ trim(g.name) }" If an operator contains characters that can occur in a variable name, then it must be separated from the other components by spaces: { n POW 2 } cannot be written as {nPOW2}, but {a + b} and {a+b} are equivalent (since '+' cannot be a character in a variable name). Data TypesMiva Script does not require that variable be declared or initialized before use, thus they are type-less. The same variable can contain numbers, text, dates, or boolean (true/false) values. In MivaScript a zero or null value is considered false, all other values are considered true. Example:<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. The type-less variables can be the source of program bugs for so care must be exercised. Both examples generate a compiler errors because of mixing string and numeric data in a single expression. However the final example works, producing a string even if all the variables are numbers, however, "9 28, 1980" is probably not the results you want. Example:<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 problems is to declare string types in the variable name itself. e.g. ( g.str_birthday ) Using ExpressionsIn Miva Script expressions are usually assigned, evaluated using <MvIF> used in a conditional, however they can also be used directly inside HTML tag attributes. In this example an Email is sent only if the form was submitted. This example demonstrates all four methods. See Operators. Example:<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 CharactersSince expressions have to be surrounded by double quotes, you have to use special techniques if you want to put double quotes inside a string expression. this is true of all non-printable characters below ascii character 32 or above character 127. You can escape characters by using the backslash or use the Miva Script asciichar() function. See ascii character list. Example:<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) }"> <MvEVAL EXPR="{ 'Dear User,' $ crlf $ 'Thank you for your interest.' }"> |