-
Notifications
You must be signed in to change notification settings - Fork 2.7k
syntax:simple
糖饼 edited this page May 6, 2014
·
5 revisions
引用简洁语法的引擎版本,例如:
<script src="dist/template.js"></script>
{{
与 }}
符号包裹起来的语句则为模板的逻辑表达式。
对内容编码输出:
{{content}}
不编码输出:
{{#content}}
编码可以防止数据中含有 HTML 字符串,避免引起 XSS 攻击。
{{if admin}}
{{content}}
{{/if}}
{{if user === 'admin'}}
{{content}}
{{else if user === '007'}}
<strong>hello world</strong>
{{/if}}
无论数组或者对象都可以用 each 进行遍历。
{{each list}}
<li>{{$index}}. {{$value.user}}</li>
{{/each}}
其中 list 为要遍历的数据字段名, $value
与 $index
是系统变量, $value
表示数据单条内容, $index
表示索引值,这两个变量也可以自定义:
{{each list as value index}}
<li>{{index}}. {{value.user}}</li>
{{/each}}
用于嵌入子模板。
{{include 'template_name'}}
子模板默认共享当前数据,亦可以指定数据:
{{include 'template_name' news_list}}
使用template.helper(name, callback)
注册公用辅助方法,例如一个基本的 UBB 替换方法:
template.helper('$ubb2html', function (content) {
// 处理字符串...
return content;
});
模板中使用的方式:
{{$ubb2html content}}
若辅助方法有多个参数使用一个空格分隔即可:
{{helperName args1 args2 args3}}
本文档针对 artTemplate v3.0.0+ 编写