[an error occurred while processing this directive]

7. Macros


Converting Macros to Miva Script Code

New Miva Script features and functions (beginning with 3.9x) provide methods to obtain certain results that were previously possible only with macros. The typical uses of macros, which can now be done with new features and functions include:

Miva Expression as Value of HTML Tag

You can assign the value of a variable to an HTML attribute or a Miva Script attribute.

One way to use a variable value is by using it in a macro expression. This allows you to use the contents of a variable as in-line HTML or Miva Script code. The syntax of a macro is:

&[ variable_name ];

When a Miva Script tag is processed, any variables found inside '&[ ...];' will be evaluated before the rest of the tag is parsed and or executed.

Note: A macro can contain only a single variable, and no expressions, literals, or functions.

The following code uses the value of server name to create a URL:

With a macro:

<MvASSIGN NAME = "server_name"> value="www.sq.com"
<A HREF="http://&[server_name];/index.html">Home Page</A>

In this example, the value of server_name will be substituted for the '&[...];'. The tag above is equivalent to:

<A HREF="http://www.sq.com/index.html">Home Page</A>

Without a macro:

<MvASSIGN NAME = "url2" VALUE = "http://www.cnn.com">
<a href ="{url2}">Click Here </a>

Macros provide the capability for "indirection" and self-generating code:

<MvASSIGN NAME="var_a" VALUE="var_b">
<MvASSIGN NAME="var_b" VALUE="burrito">
<MvEVAL EXPR="{&[var_a];}">

'<MvEVAL EXPR="{&[var_a];}">' will display the value burrito. This may seem surprising at first, but you should consider the order in which the Miva Script code is evaluated. First, the macro substitution takes place: in this step, the expression '&[var_a];' is replaced by the current value of var_a. This is the string, not the variable, 'var_b'. The <MvEVAL...> tag becomes, in effect:

<MvEVAL EXPR="{var_b}">

Then, the resulting <MvEVAL...> is executed. The EXPR '{var_b}' is evaluated, and its value, the string 'burrito', is displayed.

miva_variable_value Function

Instead of using a macro, for the above example, you can use the function, miva_variable_value, which takes a variable name and returns the value of the variable to which the variable refers.

<MvASSIGN NAME="var_a" VALUE="var_b">
<MvASSIGN NAME="var_b" VALUE="burrito">
<MvEVAL EXPR = "{miva_variable_value(var_a)}">

The value, burrito, is returned.

Many of the functions of macros can also be carried out using the string concatenation operator, '$'. For example:

<MvASSIGN name="firstname" value="Alex">

<MvASSIGN name="lastname" value="DeLarge">

<MvEVAL expr="&[lastname];, &[firstname];">

<MvEVAL expr="{lastname $ ', ' $ firstname"}>

The two <MvEVAL> tags in this example each display the same thing: 'DeLarge, Alex'.