Templates syntax: CTPL Reference Manual

## Templates syntax

CTPL supports 2 kinds of data:

Raw data | The actual content of your template, or at least most of it: unparsed data that will remain (almost) unchanged.
Template blocks | The instructions that makes the template engine useful.

### Raw data

There is no syntax for the raw data: everything that is not a template block is raw data. The only exception to this rule is that the template-blocks-delimiters `{` and `}` must be escaped using a backslash (`\`), as well as backslashes themselves. For example, to produce an opening bracket, you need to write `\{`, and to produce a backslash you need to write it twice: `\\` (the first one escapes the second).

Even if escaping a character that doesn't need to be escaped works perfectly (thought the escaping backslash doesn't appear), it is recommended not to do so in order to allow future addition of new escaping sequences without changes on your template's output.

### Template blocks

Template blocks are delimited with brackets: they are opened with an unescaped opening bracket (`{`) and closed with an unescaped closing bracket (`}`). All data inside these two brackets is template instructions.

There are 3 instruction types:

The `for` loop | The `for` loop allows to iterate over an expression that expands to an array, and computes the loop body for each element in that array. The syntax is the following:
    1
    {for <iterator> in <expression>}<loop body>{end}
`iterator` is the variable name referring to the current array's element; `expression` is an expression (see below) that expands to the array over which iterate; `loop body` is the data that will be evaluated on each iteration of the loop, and may contain any elements (raw data or instructions).
The `if` switch | The `if` block is a conditional branching. The syntax is the following:
    1
    {if <expression>}<if body>[{else}<else body>]{end}
`expression` is an expression (see below) that will be evaluated to a boolean value, `TRUE` or `FALSE`. If the `expression` evaluates to `TRUE`, the `if body` will be parsed. If the `expression` evaluates to `FALSE`, the `else body` will be parsed if any, or nothing will be done if the `else` is missing.
An expression | An expression that will be replaced by its computation result. See CtplLexerExpr for details on the syntax of expressions. Basically, it may be any mathematical-like expression that may include reference(s) to variable(s). Variable can also be indexed with a C-like syntax if they expand to an indexable type (basically, an array). The index expression must expand to an integer or compatible.

### Examples

Example 2. Short template

    1
    2
    3
    {for i in array}
      {i}
    {end}

This example will output each item of `array` on a newline. Supposing the array `[1, 2, 3]`, the output will be:

    1
    2
    3
    4
    5
    1

    2

    3

Example 3. Longer template

Here what may be a template for a member list HTML page:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <html>
      <head>
        <title>Member list</title>
      </head>
      <body>
        <h1>List of our members<h1>
        <ul class="memberlist">
        {for member in members}
          <li>{member}</li>
        {end}
        </ul>
      </body>
    </html>

Example 4. Array indexation

    1
    {array[2]}

This example will output the 3rd element (array indexes starts at zero) of the array named `array`.