Checkmark

Thank you for your feedback

RTML - Local variables

Learn about using local variables in RTML.

Article ID: SLN18719

Sometimes you may want to hold onto some value for use later on in the same template. You can do this by storing the value in a local variable. Here is a simple example where a sum is stored in a variable and later printed out:

Sum ()
TITLE "Lucky"
BODY
   WITH= variable x
      value 2 + 5
TEXT x

The WITH= operator is another one that can contain expressions. Within any of those expressions, the variable given as the variable argument will refer to the value given as the value argument. So when we get to the expression

TEXT x

it will print 7.

A variable can have anything as its value, even an image. Here is a template that solves a common problem: how to make page elements the same width:

Lineup ()

TITLE "Underscored Greeting"
BODY
   WITH= variable im
      value RENDER text "Hello"
        font-size 60
   CENTER
      IMAGE source im
      LINEBREAK
      HRULE width WIDTH im

It generates a page containing a centered image of the word Hello rendered in 60-point Helvetica, and beneath it a horizontal rule exactly the same width.

There are three new operators here. CENTER causes everything generated by the expressions it contains to be centered; HRULE generates a horizontal rule; and WIDTH returns the width of its argument.

Because we put the image in the local variable im instead of making the RENDER expression simply be the source argument of IMAGE, we can still find the width of the image after it has been inserted in the page.

See Also: