home

Softsteel Solutions

About Us Contact Us Newsletter Training
Tutorials
 

Lesson 10: Using WML variables

Unlike HTML, WML supports a limited use of variables (note that this has nothing to do with the scripting language WMLScript - these variables can be used in WML proper). This lesson covers how one uses WML variables.

Variable names

A variable name must begin with either a letter or an underscore ( _ ) character. For instance, the following two names are valid:

myVar

_myVar

Subsequent characters in the variable name can consist of any combination of letters, digits and underscores. For instance, the following two names are valid:

___123

a1_2_3

As with WML generally, variable names are case-sensitive.

Setting variables

WML variables hold string values only.

The scope of all WML variables is global to the browser environment. So cards from completely different domains can overwrite each other's variables.

To set a variable in a WML page, the <setvar/> tag is used. This tag takes a 'name' and a 'value' attribute, both of which are required. For example, the following code sets the variable called 'us' to the value 'Softsteel Solutions':

<setvar name="us" value=" Softsteel Solutions"/>

There are, however, strict rules on where the <setvar/> tag can be used. It should only be included inside either the 'go' element that we covered last chapter, or (more usually) the 'refresh' element. The refresh element is just used to specify the change in value of a variable, and to update the browser screen in respect of the change. There are rules in turn about where one can place the refresh element, these being only inside 'onevent' or 'do' elements (both lesson 8). So, for instance, one might set a greeting variable on the user navigating backwards to a card using the following piece of code

1.

<do type=prev>

2.

<refresh>

3.

<setvar name="greeting" value="welcome back">

4.

</refresh>

5.

</prev>

6.

</do>

Using variables

Once a variable has been set, it can be used by writing its name, prefixed by a dollar sign, into the WML code. When the browser comes across such a term, it replaces it with the value of the named variable. For example, given the variable assignment above, the line of code

<p>Hello from $us</p>

will appear on the browser as

Hello from Softsteel Solutions

Alternatively, one can wrap the variable name in brackets. The following gives just the same output as before:

<p>Hello from $(us)</p>

Using these brackets is better than not. With the variable name in brackets the browser can never be unsure as to what in the code is part of the variable name, and what is just plain text.

In addition, one can also specify a 'conversion' for one's variable. Such a conversion relates to the use of variables in URLs (Uniform Resource Locators). Because some string characters are not allowable in URLs, to include a variable value in a URL one should 'escape' the string - this replaces the disallowed characters with alternatives. Turning the escaped string back into its original format is called 'unescaping'.

To specify a conversion for a variable, one appends the variable name with a colon and adds a conversion literal. The following terms show our variable 'us' with conversions:

$(us:escape) or $(us:e)

- escapes the value of 'us'

$(us:unesc) or $(us:u)

- unescapes the value of 'us'

$(us:noesc) or $(us:n)

- specifies that the variable should not be escaped (this stops any browser escaping the variable by default).

It is probably best always to specify a conversion wherever variables are used. This gives one complete control over how the variable value is interpolated, and overrides any browser defaults.

 

WML Tutorials