MiniTemplator is a compact, optimized template engine primarily used for generating HTML output.
Variables:
${variableName}
Blocks:
<!-- $beginBlock blockName --> ... block content ... <!-- $endBlock blockName -->
Conditional statements:
<!-- $if condExpr --> ... <!-- $elseIf condExpr --> ... <!-- $else --> ... <!-- $endIf -->
Include a subtemplate:
<!-- $include fileName -->
- Blocks can be nested.
- Subtemplates can include other subtemplates.
- Conditions are JavaScript expressions that use condition variables.
There are two phases when using MiniTemplator templates.
When a template is parsed, condition expressions are evaluated, conditional statements are resolved and subtemplates are included. A template is normally loaded and parsed only once and then used many times. A parsed template can be cached in memory for later re-use.
In the second phase, template variables are set and blocks are added. When the document buildup is complete, everything is merged into a HTML string, which is the output of the template engine.
There are two kinds of variables.
Condition variables are used in $if
and $elseIf
statements.
- Condition variables are used at the time a template is loaded and parsed.
- For each set of condition variable values, a separate parsed template object is cached.
Template variables are used to place content into the template.
- Template variables are used when applying a parsed template to generate output.
- When a template variable is used within a block, it must be set before the
addBlock()
method for the block is called. - The values
undefined
andnull
are converted into an empty string.
When the shortFormEnabled
option is set to true
, the following alternative form can be used for conditional statements:
<$? condExpr> ... content for "if" case ... <$: condExpr> ... content for "elseIf" case ... <$:> ... content for "else" case ... <$/?>
Example:
<$?de> Hallo Welt! <$:fr> Bonjour le monde! <$:it> Ciao mondo! <$: > Hello world! <$/?>