home
features
quick start
download
documentation
thymol files
configuration
message resources
control parameters
expression objects
thymol types
thymol API
thymol extensions
examples
sessions
say-hello
stsm example
with-dialect
extrathyme
petclinic
news
credits
user forum
issues
browser constraints
other known issues
create new issue
thymeleaf

thymoljs

thVars

There are two ways of defining and initialising Thymol variables, by using request parameters or via JavaScript declarations.

The value of an expression variable may be defined through the use of request parameters attached to the URL by which the static template is invoked.

Request parameters are specified by using the usual "http get" mechanism:

      file:///C:/Users/jjb/project/big-example/page5.html?firstName=Fred&surName=Bloggs

Thymol allows the definition of variables as structures and name/value pairs using a JavaScript string array with a json-like syntax.

The Thymol variables definition JavaScript array must be given the identifier "thVars".

Here is an example:

        var thVars = [
           ...
            [ "home",            "/home/fred" ],
            [ "home",            "/home/fred" ],
            [ "valueIndex",    345 ],
            [ "product",         "#{ 'name': 'Lettuce', 'prices': { 'euros': '9.00', 'dollars': '12.00' } }" ],
           ...
        ];

This example causes the creation of three Thymol variables.

The first is called "home" and has the string value "/home/fred".

The second is called "valueIndex" and has a primitive integer value of 345.

The third is an object variable called "product". The "product" object has two properties, a scalar called "name" and a map called "prices".

The "name" field has a primitive string value of "Lettuce".

The "prices" map comprises two fields: "euros" and "dollars" with values 9.00 and 12.00 respectively.

Following their definition, we may refer to these variables in expressions by using the terms "${home}", "${valueIndex}" or "${product}" respectively.

The values of the "product" object's internal properties may be referenced using the familiar "dot" notation ("${product.prices.euros}" etc ).

Alternatively, expressions may refer to properties of the "product" object using the associative-array style as "${product['name']}".

collections

Collections are also supported in the thVars structure.

To define an array of text values use the following json-like syntax:

        var thVars = [
           ...
            ["types",            "#[ 'Cat', 'Dog', 'Bird', 'Reptile', 'Amphibian' ]"],
           ...
        ];

Thymol collection object members may be referenced using (zero based) index values.

For example, with the above thVars definition, the following span should be rendered with the value "Bird":

        <span th:text="${types[2]}">default</span>

Thymol collections are most useful for prototyping values to be used in elements with Thymeleaf th:each attributes.

object references

The Thymol thVars structure allows the definition and use of object references in the "style" of OGNL by prefixing the name of the object variable with a single "#" character.

        var thVars = [
           ...
            ["c1",               "#{ 'key': 'Galicia',    'value': 'Santiago de Compostela' }"],               
            ["c2",               "#{ 'key': 'Asturias',    'value': 'Oviedo' }"],               
            ["c3",               "#{ 'key': 'Cantabria',    'value': 'Santander' }"],                        
            ["capitals",       "#[ #c1, #c2, #c3 ]"],
            ["data",            "#{ 'regions': #capitals, 'extras': {} }",
           ...
        ];

In the above example, we have defined a list of objects called "capitals" that holds three name/value pairs,

Thymol object references may be used in the thVars structure wherever a variable name may be used, but not within templates.

Thymol object references may be used as URL parameters, but the "#" prefix must be escaped (URL encoded) in order for this to work:

e.g.

           page5.html?regions=%23capitals&range=%23data

expressions

Thymol allows the use of the complete standard dialect expression syntax.

The evaluation of boolean expressions is consistent with that of Thymeleaf's ObjectUtils.evaluateAsBoolean() function.