The template language syntax is highly customizable. The default syntax is intended to make transition from lodash templates as simple as possible for escaping and non-escaping interpolation tags. An alternate syntax is described below or can be replaced entirely.
All tags are based on opening and closing sequences of <%
and %>
.
Syntax:
<%- expression [ | filter ... ] %>
Syntax:
<%= expression [ | filter ... ] %>
Syntax:
<% if ( expression ) %>
Syntax:
<% else %>
Syntax:
<% end %>
Syntax:
<% for ( expression ; alias ) %>
Syntax:
<% end %>
The portion of the template between the for
tag and the end
tag is rendered once for each item on the collection represented by the expression value. The expression value is expected to be ‘array like’. The current item from the collection is set using the alias parameter as the property name. In addition, the following properties are set in the current data model:
Note: _first and _last will both be true for one item arrays. For example, to emit a comma between all items, use the else clause of a conditional:
const overtemplate = require('@mscalora/overtemplate');
template = '<% for (letters;item)%><%- item %><% if (item_last) %><% else %>, <% end %><% end %>',
compiled = overtemplate(template);
console.log(compiled({letters: Array.from('abc')}));
> a, b, c
The alternate syntax is provided for use when separator characters used in the default syntax might cause conflicts with template data.
Syntax:
«- expression [ ~ filter ... ] »
Syntax:
«= expression [ ~ filter ... ] »
Syntax:
« if ( expression ) »
Syntax:
« else »
Syntax:
« end »
Syntax:
« for ( expression • alias ) »
Syntax:
« end »
See the loop documentation of the default syntax section documents for usage information.
Note: The default parameter separator character for the alternate syntax is the bullet character (Mac keystroke: option-8) and the filter separator is a tilde character. The tag beginning and end characters are accessed from a typical US-EN keyboard layout with option-\ and option-shift-\.
Syntax customization is achieved by replacing one or more built-in regular expressions. You should be familiar with non-greedy quantifiers, non-capturing groups, negative assertions and the capture features of regular expressions. The following settings are customizable:
For syntax customization, using the Named Groups option is recommended by setting the namedGroups to true
Here’s an example of replacing the entire tag syntax to replace <%
and %>
with {$
and $}
const DEFAULT_ALT_NAMED_CAPTURE_PARSING = {
namedGroups: true,
escape: /\{\$-(?<escape>[\s\S]+?)\$}/g,
interpolate: /\{\$=(?<interpolate>[\s\S]+?)\$}/g,
terminate: /\{\$\s*?(?<terminate>end)\s*?\$}/g,
conditional: /\{\$\s*?if\s*?\((?<conditional>(?:.(?!\$}))+?)\)\s*?\$}/g,
alternative: /\{\$\s*?(?<alternative>else)\s*?\$}/g,
loop: /\{\$\s*?for\s*?\((?<loopArray>(?:.(?!\$}))+?);(?<loopAlias>(?:.(?!\$}))+?)\)\s*?\$}/g,
};
Also, if you wanted to change the loop structure to:
{$ loop
alias in
array $}
loop: /\{\$\s*?loop\s+?(?<loopAlias>(?:.(?!\$}))+?)\s+?in\s+?(?<loopArray>(?:.(?!\$}))+?)\s+?\$}/g
You would use:
settings = {
escape: /\[#(?<escape>(?:.(?!%>))+?.)#]/g,
interpolate: /\[\$(?<interpolate>(?:.(?!\$>))+?.)\$]/g,
}
Each regexp must capture its settings key except for the loop which must capture loopArray and loopAlias.
Note:
Named group capture is a feature that was introduced in ECMAScript 2018, it is only used by this module if you use the settingnamedGroups
set to true.
If named group capture is NOT in use, then each syntax parsing regexp must have exactly one capturing group except for the loop tag which must have exactly two capturing groups with the array before the alias. Any missing or extra captures will break parsing and reversing the loop parameters like the example above is not possible.