template.defaults.rules
You can customize template parsing rules in art-template. Standard syntax and orginal syntax is configured by default.
Modify delimiters
// delimiter rules of original syntax |
They are regular expressions and you can only modify the delimiter part. For example, modify <%
%>
to <?
?>
:
var rule = template.defaults.rules[0]; |
Add syntax
Let’s start with a simple example that make template engine support parse of template strings ${name}
of ES6:
template.defaults.rules.push({ |
test
is a regular expression which matches strings and use
is a callback function after matching. About use
function:
- parameters: first parameter is the matching string, and others are content of capturing group of
test
regular expression - return value: MUST return an object containing
code
andoutput
properties:code
transformed JavaScript statementsoutput
describe type ofcode
, optional value:'escape'
output after encoding'raw'
output raw contentfalse
output nothing
It’s worth mentioning that syntax rules have no effect on rendering speed and template parser will help you optimize rendering performance.