doT.js 是一个 JavaScript 模板框架,在 web 前端使用 dot.js 作为模板引擎,主要的用途就是,在写好的模板上,放进数据,生成含有数据的 html 代码。
和后端渲染不同,doT.js 的渲染完全交由前端来进行,这样做主要有以下好处:
<script type="text/javascript" src="./js/doT.min.js"></script>
npm install dot
然后在代码中使用
require('dot')
{{=it.xxx }}
{{=xxx }}
{{~it.array :value:index}} ...{{~}}
javascript {{ for var key in data { }} {{= key }} {{ } }}
{{=(it.value=='somevalue'?'value1':'value2')}}
{{? }} 表if{{??}}表else if{{??}}表else
{{!it.xxx}}
{{# }} for compile-time evaluation/includes and partials
{{## #}} for compile-time defines
{{=it.xxx }}
用法:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="./js/doT.min.js"></script>
<script type="text/javascript" src="./js/jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="showHtml"></div>
<script type="text/javascript">
var data = {"name":"Jake","age":31};
var str = "Hi {{=it.name}}!{{=it.age || ''}}"
var tempFn = doT.template(str);
$("#showHtml").html(tempFn(data));
</script>
</body>
</html>
输出:
{{=xxx }}
和 遍历对象 用法:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="./js/doT.min.js"></script>
<script type="text/javascript" src="./js/jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="showHtml"></div>
<script type="text/javascript">
var data = {"name":"Jake","age":31,"mother":"Kate","father":"John",
"interests":["basketball","hockey","photography"],"contact":{"email"
:"jake@xyz.com","phone":"999999999"}};
var str = "{{ for(var prop in it) { }}{{=prop}}{{ } }}"
var tempFn = doT.template(str);
$("#showHtml").html(tempFn(data));
</script>
</body>
</html>
输出:
{{~it.array :value:index}} ...{{~}}
用法:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="./js/doT.min.js"></script>
<script type="text/javascript" src="./js/jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="showHtml"></div>
<script type="text/javascript">
var data = {"array":["banana","apple","orange"]};
var str = "{{~it.array:value:index}}{{=value}}!{{~}}"
var tempFn = doT.template(str);
$("#showHtml").html(tempFn(data));
</script>
</body>
</html>
输出:
{{=(it.value=='somevalue'?'value1':'value2')}}
用法<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="./js/doT.min.js"></script>
<script type="text/javascript" src="./js/jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="showHtml"></div>
<script type="text/javascript">
var data = {"name":"Jake","age":31};
var str = "{{=(it.name=='Tom'?'我':'他')}}"
var tempFn = doT.template(str);
$("#showHtml").html(tempFn(data));
</script>
</body>
</html>
输出:
{{? }} 表if{{??}}表else if{{??}}表else
用法:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="./js/doT.min.js"></script>
<script type="text/javascript" src="./js/jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="showHtml"></div>
<script type="text/javascript">
var data = {"name":"Jake","age":31};
var str = "{{? !it.name == 'Tom'}} 我叫Tom" +
"{{??}} 我叫Jake{{?}}";
var tempFn = doT.template(str);
$("#showHtml").html(tempFn(data));
</script>
</body>
</html>
输出:
以上是doT.js的基础用法。