CTPL supports 2 kinds of data:
The actual content of your template, or at least most of it: unparsed data that will remain (almost) unchanged. |
|
The instructions that makes the template engine useful. |
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 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 |
The The syntax is the following:
|
||
The |
The The syntax is the following:
If the |
||
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. |
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
.