art-template supports standard syntax and original syntax. Standard syntax allows templates to be easier to read and write. While original syntax has powerful logical processing ability.
Standard syntax supports basic templating syntax and JavaScript expression. Original syntax supports arbitrary JavaScript statement, the same as EJS.
Output
standard syntax
{{value}} |
original syntax
<%= value %> |
You can use $data
with bracket notation to access a first-class variable of templates (such as keyword, reserved word, etc):
{{$data['user list']}} |
Raw output
standard syntax
{{@ value }} |
original syntax
<%- value %> |
raw output will not escape the content of
HTML
, so there may be security problems. Please be careful.
Condition
standard syntax
{{if value}} ... {{/if}} |
original syntax
<% if (value) { %> ... <% } %> |
Loop
standard syntax
{{each target}} |
original syntax
<% for(var i = 0; i < target.length; i++){ %> |
target
supports iteration ofarray
andobject
, and its default value is$data
.$value
and$index
can be customized:{{each target val key}}
.
Variable
standard syntax
{{set temp = data.sub.content}} |
original syntax
<% var temp = data.sub.content; %> |
Template inheritance
standard syntax
{{extend './layout.art'}} |
original syntax
<% extend('./layout.art') %> |
Template inheritance allows you to build a basic templating “skeleton” that contains common parts of your site. Example:
<!--layout.art--> |
<!--index.art--> |
After rendering index.art, layout skeleton will be automatically applied.
Sub template
standard syntax
{{include './header.art'}} |
original syntax
<% include('./header.art') %> |
data
value is$data
by default. Standard syntax doesn’t support declaration ofobject
andarray
but reference variable. However, original syntax is no limits.- art-template has built-in HTML minifier and please avoid writing abnormal closing tag in sub templates. Otherwise, tags may be unexpectedly “optimized” when
minimize
option is open.
Filters
register filters
template.defaults.imports.dateFormat = function(date, format){/*[code..]*/}; |
The first parameter of filter function accepts target value.
standard syntax
{{date | timestamp | dateFormat 'yyyy-MM-dd hh:mm:ss'}} |
{{value | filter}}
filter syntax is similar to pipe operator. Its last output will be the next input.
original syntax
<%= $imports.dateFormat($imports.timestamp(date), 'yyyy-MM-dd hh:mm:ss') %> |
If you want to modify
{{
}}
and<%
%>
,please refer to rules.