Template engine

ITTF Documents are composable templates, and their nodes can contain template commands and JsWizzi expressions.

The buildup of an mTree can repeat node branches using commands like '$foreach', '$backeach' and '$while' and can include or exclude node branches using '$if', '$elif' and '$else' commands.

JsWizzi statements and expressions are executed in the evaluation context of an ITTF Document code unit (file) and may access global context variables.

The $foreach command

                                
1 ...
2 $foreach item-name in collection-name
3 ... node branch ...

Descendant nodes of this command are a repeat template; item-name is user defined and collection-name is a JsWizzi expression that must evaluate to an iterable javascript object, in the fragment or global scope.

The var statement that declares item-name is auto-generated.

Descendant nodes are repeated having item-name as a value in scope.

The $while command

                                
1 ...
2 $while jswizzi-expression
3 ... node branch ...
4 $ jswizzi-exit-condition-change

Descendant nodes are repeated until the jswizzi-expression evaluates to false.

$ or $global commands must be used to modify the context values of the exit condition. There is a limit of 10000 iterations to guard against accidental loops, after that an exception is raised.

TODO make the limit of 10000 iterations configurable and an optional parameter of the loadMTree function.

The $break, $continue commands

                                
1 ...
2 $foreach ...
3 ...
4 $break
5 ...
6 $continue

This statements have the same meaning than in javascript and operate breaking or continuing the template repetition. Labels for continuation are not implemented.

Ittf commands
The $include command

                                
1 ...
2 $include fragment-path

The node value ( fragment-path ) is the relative path to the ITTF Fragment that must be included. The folder uri of the includer document is the base path for the path resolution of the included documents.

The fragment will be included replacing the $include command, and the JsWizzi expressions of its nodes will become part of the evaluation scope of the includer.

An $include command cannot have children nodes. The included fragment cannot have the $params node (cannot have composition parameters).

The Mix action

                                
1 ...
2 fragment-path( arg-1 [, arg-2 ... [, arg-n]])
3 ... optional child branch nodes

A Mix action is recognized by the ITTF Parser when a node name ends with an open parenthesis. The chars before the open parenthesis are the relative path to the mixed ITTF Document. The folder uri of the mixer ITTF Document is the base path. No whitechar must separate the mixed fragment path and the open paren.

The mixed fragment is inserted in the mixer document replacing the Mix action. If the mix node has children, by default they are appended to the root node of the mixed fragment. But if the mixed fragment has a descendant node that is a '$hook' command of name 'default', than the children of the mix node will replace it.

ITTF Fragment with an $hook command

                                
1 table
2 $hook

The hook-name when not declared, is assumed to be 'default'.

The root node of a mixed ITTF Document can be a template Mix action . A fragment can mix its container and append nodes to its hooks:


                                
1 mylayout( home, )
2 $append scripts
3 js dialogs.js
4 div My content
The $params command

                                
1 rootnode
2 $params param1 [,param2 [,param-n]]

ITTF Fragments designed to be mixed can declare composition parameters. The `$params` node must be the first child of the root node.

A parameter has format: [&]name[:type][|default]

&
Optional. A paramater name prededed by `&` means an object passed by reference
name
the name of the parameter to be used in jsWizzi expressions
type
the parameter type

one-of: string(default), integer, float, boolean, date, object (implicit when '&' declared), macro (implicit, detected from IttfMacro delimiterscontained in the default value)

default
typed-value | @@null | @@undefined

warning! `title|null` becomes title = "null" (the quoted string null).

If you want title to be null then : `title|@@null`
string
quotes are optional
boolean
true | false
date
yyyy/mm/dd
macro

an IttfMacro can be passed as a parameter to a mixin, when the parameter is referenced we have a double macro substitution

This list item fragment, lia.html.ittf, ...


                                
1 li
2 $params text, href
3 a ${text}
4 href ${href}

.. can be mixed in this way


                                
1 ul
2 lia( my home page, http://hello.it)
The $hook command

                                
1 ...
2 $hook name

An $hook command is a named insertion point where node branches can be appended. The name is optional, when missing its value is 'default'.

It has to be replaced and must not have children nodes.

See the statements `Mix` and `$append`, for how to append node branches to an $hook node.

The $append command

                                
1 ...
2 $append hook-name
3 ... node branch ...

An $append command is the root of a node branch that will be appended to an ancestor $hook of `hook-name`.The children nodes of $append will replace the $hook node.

The $group command

                                
1 $group empty-value
2 node branch ...
3 node branch ...
4 ...

An ITTF Document must have one and only one root node.

The $group command is a convenience node that may be used as the root of a fragment that must have more than one root node.

The $group node has no meaningful value. It disappear in the builded mTree.

The $if, $elif, $else commands

                                
1 ...
2 $if jswizzi-expression
3 ... node branch ...
4 $elif jswizzi-expression
5 ... node branch ...
6 $else empty-value
7 ... node branch ...

Children nodes of these commands are included/excluded from the builded mTree depending on the value of the expression.

The $foreach and $backeach commands

                                
1 ...
2 $foreach item-name in collection-name
3 ... node branch ...

Descendant nodes of this command are a repeat template; item-name is user defined and collection-name is a JsWizzi expression that evaluatesto an iterable javascript object, that must be in scope in the fragment or in the globaljsWizziContext.

The var statement that declares item-name is auto-generated.

Descendant nodes are repeated having item-name as a value in scope.

The $backeach command iterates the collection backword.

The $break, $continue commands

                                
1 ...
2 $break
3 ...
4 $continue

This statements have the same meaning than in javascript but operate breaking or continuing the template repetition.

A label for continuation is not implemented.

the $while command

                                
1 ...
2 $while jswizzi-expression
3 ... node branch ...
4 $ jswizzi-exit-condition-change

This is a repeat template statement where descendant nodes are repeated until the jswizzi-expression evaluates to false.

$ or $global commands must be used to modify the context values of the exit condition. There is a limit of 10,000 iterations to guard against accidental loops, after that an exception is raised.

Example


                                
1 ...
2 $ i = 0
3 $while i < 256
4 p Item ${i}
5 $ i++
The $ command - single line

                                
1 ...
2 $ jswizzi-statement
The $ command - multi line

                                
1 $
2 jswizzi-statement
3 jswizzi-statement
4 ...

Descendant nodes of this command can contain JsWizzi expressions that, during the evaluation step, are executed in the fragment scopeto wich the command belongs.

They may declare and modify variables that have fragment scope, and modify variables that have global scope.

The $global command - single line

                                
1 ...
2 $global jswizzi-statement
The $global command - multi line

                                
1 ...
2 $global
3 jswizzi-statement
4 jswizzi-statement
5 ...

Descendant nodes of this command can contain JsWizzi expressions that, during the evaluation step, are executed in the global scope of the jsWizziContext.They may declare or modify variables in the global scope.

The text container node: $.

                                
1 ...
2 $.
3 free content
4 free content
5 ...
The single line comment: $$

                                
1 ...
2 ul
3 $$ <rest-of-line is comment>
4 li item $$ <rest-of-line is comment>
The multiline comment: $* ... *$

                                
1 ...
2 $*
3 ul
4 li
5 *$
The $raw command

                                
1 ...
2 execute()

The $raw command is usefull when you need to write node lines skipping the template processing. Usually you manage the $raw tag preprocessing the mTree before the Wizzi Model Loading. TODO explain