Variables


A variable is an identifier which can have several values, but only one at a time. A variable must be declared before it is used. You can declare as many variables as you need. All variable declarations must occur before rule definitions in a template file. A variable declaration follows this syntax:

NEW [EXTERN] <variable_type> <variable_name> = <initial_value>;

Where <variable_name> is a valid identifier and <variable_type> represents the type of values that the variable can have. The only variable types allowed are STRING, BOOLEAN and INTEGER. The EXTERN keyword is optional, but it can only be specified on BOOLEAN variables.

An initial value must be defined for each variable. For Boolean variables, the initial value is TRUE or FALSE; for string variables, the initial value is a character string enclosed in double quotes; for integer variables, the initial value must be a valid integer expression. Here are valid variable declarations:

NEW BOOLEAN generateHeaders = TRUE;
NEW EXTERN BOOLEAN generateHtml = FALSE;
NEW STRING defaultName = "default";
NEW INTEGER indentation = 2;


Once declared, a value can be associated to a variable. The value assignation of a variable is performed by the SET rule class. This rule class follows this syntax:

<setrule_name> SET(<variable_name>, <variable_value>);

Where <setrule_name> represents the name identifying the set rule; <variable_name> represents the name of a variable that was declared previously, and <variable_value> represents the value that will be registered in the variable (overwriting the previous value); <variable_value> depends on the type of variable.

If the variable's type is STRING (meaning that <variable_name> has been declared with the STRING keyword), then <variable_value> is either a rule that will be processed or a string expression.

If <variable_value> is a rule, its output will be put in the variable. Rules of every class can assign string-typed variables. If <variable_value> is a string expression, the expression is evaluated and its result is put in the variable.

Here is an example of a SET rule, where <variable_value> is a rule:

setLanguage SET (language, english);//english is a subrule

english TEMPL "English";


Here is an example of a SET rule, where <variable_value> is a string expression:

NEW STRING english = "English"; //english is a string variable

setLanguage SET(language, STRING(english)); //STRING(english) is a string expression


These two examples produce the same output. Refer to the String expressions section for more details on string expressions.

It would be better to give <setrule_name> a name like setX, if the variable involved is named x. So, when you refer to this set rule in a parent rule, you will know which variable is involved. For instance, if we have:

templ TEMPL "$setLanguage;A new value has been assigned to the language variable.";

We know that the value of the variable named 'language' will change when processing TEMPL. The output of templ is :

A new value has been assigned to the language variable.

Because the output of a SET rule is always an empty string. The text 'English' is not displayed at this step, it is only stored in the 'language' variable. To obtain the value stored in a variable, you use a GET rule which syntax is:

<getrule_name> GET(<variable_name>);

As previously mentioned, it is a good idea to give to <getrule_name> the name 'getLanguage' if the variable involved is named 'language'. To obtain the value of language and use in a template string, we can define these rules:

displayValue TEMPL "The current language is : $getLanguage;.";

getLanguage GET(language);


If the value stored in the 'language' variable was 'English', then the output of displayValue will be:

The current language is : English.