Resolving variables

To use variables properly, you need to understand the way variables within custom content are resolved.

Firstly, variables can be used in the attributes of any command (or marker), not just <set/>.

For example, the commands:

<set var="Len" string="2in" />
<rule width="{Len}" />

will set a 2 inch rule. Because the width attribute of the rule command expects a measure, it will pre-convert any supplied string to a value before using it.

Secondly, all references to variables are resolved when the tag mapping that generated the custom content is processed. It is important to remember this when passing the value of attribute variables to generated custom markers.

For example, the following custom content attempts to generate a link target for tag mappings that do not declare an explicit id identifier:

<if var="@id" target="#IMPLIED">
    <Myref id="9999">
        Undefined &ndash; <Index ref="{@id}">{content}</Index>

In this example, the emitted Myref custom marker declares its own id attribute id="9999" and emits the Index custom marker. The Index custom marker assigns the value of the attribute variable {@id} to its own ref attribute. The value assigned to ref is #IMPLIED, the value resolved when the tag mapping that generated the custom content was processed.

A single level of nesting is allowed when resolving a reference to a variable. For example:

<set var="X#1" string="ABC"/>
<set var="Counter" value="1"/>

The string is: {X#{Counter}}

If more complex references are required, use the technique described in “Constructing variable names”.

To execute commands selectively, use the flow control techniques described in “Flow control”.

The important thing to remember is that all variables are stored as strings. It is only in interpreting those strings that distinctions between string, value and name types become significant.