The data model

As you have seen, the data model is basically a tree. The tree can be arbitrarily complicated and deep, e.g.:

(root)
  |
  +- animals
  |   |
  |   +- mouse
  |   |   |   
  |   |   +- size = "small"
  |   |   |   
  |   |   +- price = 50
  |   |
  |   +- elephant
  |   |   |   
  |   |   +- size = "large"
  |   |   |   
  |   |   +- price = 5000
  |   |
  |   +- python
  |       |   
  |       +- size = "medium"
  |       |   
  |       +- price = 4999
  |
  +- test = "It is a test"
  |
  +- whatnot
      |
      +- because = "don't know"  

The variables that act as directories (the root, animals, mouse, elephant, python, whatnot) are called hashes. Hashes store other subvariables by a lookup name.

The variables that store a single value (size, price, test and because) are called scalars.

When you want to use a subvariable, you specify its path from the root, and separate the steps with dots. To access the price of a mouse, you start from the root and go into animals, and then go into mouse then go into price. So you write animals.mouse.price. When you put the special ${...} codes around an expression like this, you are telling FreeMarker to output the corresponding text at that point.

There is one more important kind of variable: sequences. They are similar to hashes, but they don't store names for the variables they contain. Instead, they store their subvariables sequentially, and you can access them with a numerical index. For example, in this data model, animals and whatnot.fruits are sequences:

(root)
  |
  +- animals
  |   |
  |   +- (1st)
  |   |   |
  |   |   +- name = "mouse"
  |   |   |
  |   |   +- size = "small"
  |   |   |
  |   |   +- price = 50
  |   |
  |   +- (2nd)
  |   |   |
  |   |   +- name = "elephant"
  |   |   |
  |   |   +- size = "large"
  |   |   |
  |   |   +- price = 5000
  |   |
  |   +- (3rd)
  |       |
  |       +- name = "python"
  |       |
  |       +- size = "medium"
  |       |
  |       +- price = 4999
  |
  +- whatnot
      |
      +- fruits
          |
          +- (1st) = "orange"
          |
          +- (2nd) = "banana"  

To access a subvariable from a sequence you use a numerical index in square brackets. Indexes start from 0 (programming logic traditionally starts with 0), thus the index of the first item is 0, the index of the second item is 1, and so on. So to get the name of the first animal you write animals[0].name. To get the second item in whatnot.fruits (which is the string "banana") you write whatnot.fruits[1].

A scalar can store values of different types. The most commonly used types are:

Summary:


Page generated: 2006-03-15 13:49:01 GMT FreeMarker Manual -- For FreeMarker 2.3.6