3. Expressions and Literals


Introduction

Expression

An expression is a formula that can be evaluated to give a result. Expressions always occur in attribute values and must be enclosed in curly brackets, '{' and '}'. Expressions can contain variables, literal values (text or numbers), and operators, which indicate how the other components in the expression are to be evaluated.

In general, Miva Script expressions can be used in two ways:

Literal

A literal is a number or a string of characters that is not meant to be evaluated: for example: 23 and 'hi there'. Literals don't have to be enclosed in curly brackets.

In general, if an attribute value (except for a macro) needs to be evaluated, it must be enclosed in curly brackets.

When a value surrounded by curly brackets is used in an attribute value, the open bracket must immediately follow the double-quote character that starts the attribute value (that is, there must be no spaces between them; similarly, the closed bracket must immediately precede the double-quote that terminates the attribute value. If you don't do this, the expression will be treated like a literal string. For example:

<MvCOMMENT> Wrong! </MvCOMMENT>
<MvEVAL EXPR=" {a + b} ">
<MvCOMMENT> Right! </MvCOMMENT>
<MvEVAL EXPR="{a + b}">

Examples

Examples of some expressions:

{age}
{age + 1}
{age GE 17}
{x * (y / z)}
{'a' IN name}

These examples contain:

Notice also that a single variable name can be an expression. Conversely, an expression can have as many components as are required.

Inside an expression, spaces are not significant (that is, they do not affect how the expression is evaluated) except in the following ways:

One use for expressions that you've seen already is in a variable assignment:

<MvASSIGN NAME="age" VALUE="{age + 10}">

Expressions are also used in conditionals, which test some condition and (usually) take an action based on the results of the test. Here is an example:

<MvIF EXPR="{age GE 17}">
<p><b>This person is eligible to drive a motor vehicle.</b></p>
</MvIF>

This <MvIF> tag tests the expression '{age GE 17}' ("Is the age greater than or equal to 17?") and if that expression is true (which depends on the current value of age), it prints some text.

An expression consisting only of a literal string does not require curly brackets. If a literal string is inside curly brackets, it must be surrounded by single quotes (that is, the apostrophe or front quote character, '...'). The following tags are equivalent:

<MvASSIGN NAME="person" VALUE="Moira">
<MvASSIGN NAME="person" VALUE="{'Moira'}">

If you want to put a literal string of text in an expression that contains operators or variables, surround the literal with single quotes. For example:

<MvASSIGN NAME="boss" VALUE="{'Fred ' $ 'Flintstone')">

Here the literals Fred and Flintstone are surrounded by double quotes.

You should represent a null string in an expression as two single quotes (' '):

<MvIF EXPR="{entry EQ ''}">

The Miva Script comparison or logical operators (such as GE and AND) always produce a result of 0 (false) or 1 (true).

Note: An expression consisting of a literal number, for example, {149.99}, evaluates to that number.

Quotes and Other Special Characters in Expressions

Since expressions have to be surrounded by double quotes, you have to use special techniques if you want to put double quotes in expressions.

There are two ways to handle this:

If you want to assign a string of HTML (but not Miva Script) code to a variable, you can use single quotes instead of double quotes to surround attribute values. The code in the variable can then be passed to the browser using <MvEVAL>. Since the browser cannot execute Miva Script code, you should pass only HTML code to the browser in this way.

<MvASSIGN
NAME="input1"
VALUE = "Address: <INPUT TYPE='text' NAME='address'>">
<FORM
ACTION = "http://www.sq.com/cgi-bin/quagmire"
METHOD = "post">
<MvEVAL EXPR = "{input1}">
</FORM>

This displays:

Top of Form





Address:



Bottom of Form

The code in this example assigns a string to a variable named input1. The value of input1 is HTML code that generates a text box with the label "Address:". Miva will evaluate input1 to send the HTML code to the browser; it also creates a Miva Script global variable called address (or g.address).

If you really have to use double quotes, you can use the built-in function asciichar(n) to represent the double quotes. asciichar(n) is equivalent to the character whose ASCII code is n. The code for a double quote happens to be 34. You will also need to use the concatenation operator, $, to join this character to the rest of your text. For example:

<MvASSIGN
NAME="banner"
VALUE="'Home of '$asciichar(34)'$The Best
Wings in Town'$asciichar(34)'$!'">
<MvEVAL EXPR="{banner}">

This displays:

Home of "The Best Wings in Town"!

Notice that the literal parts of the expression have to be surrounded by single quotes.

Other Special Characters

You can use the asciichar(n) function to represent other characters that you cannot use literally in an expression, for example, carriage return and line feed. If you will be using these characters often you can assign asciichar(n) to a variable and use the variable instead. For example:

<MvASSIGN NAME="dq" VALUE="{asciichar(34)}">
<MvASSIGN NAME="cr" VALUE="{asciichar(13)}">
<MvASSIGN NAME="lf" VALUE="{asciichar(10)}">
<MvASSIGN NAME="crlf" VALUE="{asciichar(13)$asciichar(10)}">
<MvEVAL EXPR="{'Dear User,'$crlf$'Thank you for your interest.'}">

Displaying the Result of an Expression

<MvEVAL>

Summary

Evaluates and displays the value of expression.

<MvEVAL EXPR="{expression}">

There are many ways of evaluating an expression, but if you want to display the result in the browser, you should use the <MvEVAL> tag.

For example:

<p><b>The final amount is: <MvEVAL EXPR="{price*units}">.</b><p>

Another way to think about the <MvEVAL> tag is that it is replaced, in an HTML document, with the result of an expression.

The <MvEVAL> tag has one attribute, EXPR, whose content can be an expression to be evaluated, or a literal string or number. If the content is an expression it must be enclosed in curly brackets, or it will be treated like a literal and not evaluated. Normally, if you want to display a literal you would just enter it directly in the HTML file, rather than using an <MvEVAL>.

When multiple fields in a form are given the same name (e.g. "name"), return values will be comma delimited. For example, the following:

name1<INPUT TYPE="text" NAME="name"><BR>
name2<INPUT TYPE="text" NAME="name"><BR>
name3<INPUT TYPE="text" NAME="name"><BR>
name4<INPUT TYPE="text" NAME="name">
<INPUT TYPE="submit VALUE="Submit Query">

The results of "<MvEVAL EXPR="{ name }">" is "1,2,,4."

Miva expressions are also evaluated inside HTML tags, if the string is bound by curly braces, and uses the SGML format name = "value"

<MvASSIGN NAME="l.textvalue" VALUE="default value">
<INPUT NAME="inputname" VALUE="{l.textvalue}" TYPE="text">

Results in an input with the default value of "default value".

Also see "Multiple Form Variables with the Same Name" on page�28, for an example of the variable can be treated as an array.

For information on accessing array elements, see Chapter 5, "Arrays and Structures."