简介: The fastest + concise javascript template engine for Node.js and browsers.
译文: Node.js和浏览器环境下最快+简洁的javascript模板引擎
文档:http://olado.github.io/doT/index.html
下载
# 开发版 5.1K
wget https://raw.githubusercontent.com/olado/doT/master/doT.js
# 压缩版 3.3K
wget https://raw.githubusercontent.com/olado/doT/master/doT.min.js
Node.js环境使用
npm install dot
浏览器使用
基本模板语法
{{ }} for evaluation
{{= }} for interpolation
{{! }} for interpolation with encoding
{{# }} for compile-time evaluation/includes and partials
{{## #}} for compile-time defines
{{? }} for conditionals
{{~ }} for array iteration
基本使用
{{=it.name}}
var data = {
name: "Tom",
};
var template = doT.template(document.getElementById("content-tmpl").text);
// 显示渲染结果
document.getElementById("content").innerHTML = template(data);
更多使用示例
Document
赋值
{{=it.name}}
循环map
{{for(var key in it.attrs) { }}
{{=key}}: {{=it.attrs[key]}}
{{ } }}
数组
{{~it.pets:value:index}}
{{=index}}: {{=value}}
{{~}}
条件
{{? it.sex == 0}}
女
{{??}}
男
{{?}}
var data = {
name: "Tom",
sex: 0,
pets: ["dog", "cat", "pig"],
attrs: {
age: 23,
phone: 123456,
},
};
var template = doT.template(document.getElementById("content-tmpl").text);
document.getElementById("content").innerHTML = template(data);
参考资料 http://www.jq22.com/jquery-info8648