doT.js详细介绍

doT.js详细介绍

doT.js特点是快,小,无依赖其他插件。

官网:
http://olado.github.iodoT.js详细使用介绍

使用方法:
{{= }} for interpolation
{{ }} for evaluation
{{~ }} for array iteration
{{? }} for conditionals
{{! }} for interpolation with encoding
{{# }} for compile-time evaluation/includes and partials
{{## #}} for compile-time defines

调用方式:
var tmpText = doT.template(模板);
tmpText(数据源);

例子一:

1、for interpolation 赋值
格式:
{{= }}

数据源:{"name":"Jake","age":31}

区域:<div id="interpolation"></div>

模板:

<script id="interpolationtmpl" type="text/x-dot-template">
<div>Hi {{=it.name}}!</div>
<div>{{=it.age || ‘‘}}</div>
</script>

调用方式:

var dataInter = {"name":"Jake","age":31};
var interText = doT.template($("#interpolationtmpl").text());
$("#interpolation").html(interText(dataInter));

例子二:

2、for evaluation for in 循环
格式:
{{ for var key in data { }} 
{{= key }} 
{{ } }}

数据源:{"name":"Jake","age":31,"interests":["basketball","hockey","photography"],"contact":{"email":"[email protected]","phone":"999999999"}}

区域:<div id="evaluation"></div>

模板:

<script id="evaluationtmpl" type="text/x-dot-template">
{{ for(var prop in it) { }}
<div>KEY:{{= prop }}---VALUE:{{= it[prop] }}</div>
{{ } }}
</script>

调用方式:

var dataEval = {"name":"Jake","age":31,"interests":["basketball","hockey","photography"],"contact":{"email":"[email protected]","phone":"999999999"}};
var evalText = doT.template($("#evaluationtmpl").text());
$("#evaluation").html(evalText(dataEval));

例子三:

3、for array iteration 数组
格式:
{{~data.array :value:index }}
...
{{~}}

数据源:{"array":["banana","apple","orange"]}

区域:<div id="arrays"></div>

模板:
<script id="arraystmpl" type="text/x-dot-template">
{{~it.array:value:index}}
<div>{{= index+1 }}{{= value }}!</div>
{{~}}
</script>

调用方式:

var dataArr = {"array":["banana","apple","orange"]};
var arrText = doT.template($("#arraystmpl").text());
$("#arrays").html(arrText(dataArr));

例子四:

4、{{? }} for conditionals 条件
格式:
{{? }} if
{{?? }} else if
{{??}} else

数据源:{"name":"Jake","age":31}

区域:<div id="condition"></div>
模板:
<script id="conditionstmpl" type="text/x-dot-template">
{{? !it.name }}
<div>Oh, I love your name, {{=it.name}}!</div>
{{?? !it.age === 0}}
<div>Guess nobody named you yet!</div>
{{??}}
You are {{=it.age}} and still dont have a name?
{{?}}
</script>

调用方式:

var dataEncode = {"uri":"http://bebedo.com/?keywords=Yoga","html":"<div style=‘background: #f00; height: 30px; line-height: 30px;‘>html元素</div>"};
var EncodeText = doT.template($("#encodetmpl").text());
$("#encode").html(EncodeText(dataEncode));

例子五:

5、for interpolation with encoding
数据源:{"uri":"http://bebedo.com/?keywords=Yoga"}

格式:
 {{!it.uri}}

区域:<div id="encode"></div>

模板:
<script id="encodetmpl" type="text/x-dot-template">
Visit {{!it.uri}} {{!it.html}}
</script>

调用方式:

var dataEncode = {"uri":"http://bebedo.com/?keywords=Yoga","html":"<div style=‘background: #f00; height: 30px; line-height: 30px;‘>html元素</div>"};
var EncodeText = doT.template($("#encodetmpl").text());
$("#encode").html(EncodeText(dataEncode));

例子六:

6、{{# }} for compile-time evaluation/includes and partials
{{## #}} for compile-time defines

数据源:{"name":"Jake","age":31}

区域:<div id="part"></div>

模板:

<script id="parttmpl" type="text/x-dot-template">
{{##def.snippet:
<div>{{=it.name}}</div>{{#def.joke}}
#}}
{{#def.snippet}}
{{=it.html}}
</script>

调用方式:

var dataPart = {"name":"Jake","age":31,"html":"<div style=‘background: #f00; height: 30px; line-height: 30px;‘>html元素</div>"};
var defPart = {"joke":"<div>{{=it.name}} who?</div>"};
var partText = doT.template($("#parttmpl").text(), undefined, defPart);
$("#part").html(partText(dataPart));

时间: 2024-08-27 09:52:12

doT.js详细介绍的相关文章

doT.js详细使用介绍

官网: http://olado.github.iodoT.js详细使用介绍 使用方法: {{= }} for interpolation {{ }} for evaluation {{~ }} for array iteration {{? }} for conditionals {{! }} for interpolation with encoding {{# }} for compile-time evaluation/includes and partials {{## #}} for

doT.js详细解析

doT.js特点是快,小,无依赖其他插件. 官网:http://olado.github.io 使用方法: 1){{= }} for interpolation 2){{ }}   for evaluation 3){{~ }}  for array iteration 4){{? }}  for conditionals 5){{! }}  for interpolation with encoding 6){{# }}  for compile-time evaluation/include

js中的json对象详细介绍

JSON一种简单的数据格式,比xml更轻巧,在JavaScript中处理JSON数据不需要任何特殊的API或工具包,下面为大家详细介绍下js中的json对象, 1.JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不需要任何特殊的API或工具包. JSON的规则很简单:对象是一个无序的“‘名称:值'对”集合.一个对象以“{”(左括号)开始,“}”(右括号)结束

JS 事件绑定、事件监听、事件委托详细介绍

事件绑定 要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有三种常用的绑定事件的方法: 在DOM元素中直接绑定: 在JavaScript代码中绑定: 绑定事件监听函数. 在DOM中直接绑定事件 我们可以在DOM元素上绑定onclick.onmouseover.onmouseout.onmousedown.onmouseup.ondblclick.onkeyd

PHP模板引擎smarty详细介绍

篇文章主要介绍了PHP模板引擎smarty详细介绍,本文讲解了什么是smarty.smarty优点.不适合使用smarty的地方.smarty目录结构及版本,需要的朋友可以参考下 /* 一.什么是smarty? smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲, 目的就是要使用PHP程序员同美工分离,使用的程序员改变程序的逻辑内容不会影响到美工的页面设计,美工重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要. 二.smarty优

doT.js 模板引擎的使用

模版引擎之前在介绍mustache时已经提到了.doT.mustache各有优势吧,如果用于JS环境,doT的性能更胜一筹,并且套语句更方便些.现在公司的mobile页面已经被我替换成doT了. doT source: https://github.com/olado/doTDocs: http://olado.github.com/doT/ doT上手比较容易,但之前熟悉了mustache,改用doT时有点不习惯,但用两次就好了. 下载doT.js(里面有个doU.js不要用,doU.js是为

HTML页面加载和解析流程详细介绍

浏览器加载和渲染html的顺序.如何加快HTML页面加载速度.HTML页面加载和解析流程等等,在本文将为大家详细介绍下,感兴趣的朋友不要错过 浏览器加载和渲染html的顺序 1. IE下载的顺序是从上到下,渲染的顺序也是从上到下,下载和渲染是同时进行的. 2. 在渲染到页面的某一部分时,其上面的所有部分都已经下载完成(并不是说所有相关联的元素都已经下载完). 3. 如果遇到语义解释性的标签嵌入文件(JS脚本,CSS样式),那么此时IE的下载过程会启用单独连接进行下载. 4. 样式表在下载完成后,

JavaScript执行顺序详细介绍

JavaScript执行顺序详细介绍 作者: 字体:[增加 减小] 类型:转载 时间:2013-12-04我要评论 这篇文章主要介绍了JavaScript执行顺序,有需要的朋友可以参考一下 之前从JavaScript引擎的解析机制来探索JavaScript的工作原理,下面我们以更形象的示例来说明JavaScript代码在页面中的执行顺序.如果说,JavaScript引擎的工作机制比较深奥是因为它属于底层行为,那么JavaScript代码执行顺序就比较形象了,因为我们可以直观感觉到这种执行顺序,当

Linux Bash内置命令大全详细介绍

转自:http://os.51cto.com/art/201006/207329.htm 主要Shell内置命令 Shell有很多内置在其源代码中的命令.这些命令是内置的,所以Shell不必到磁盘上搜索它们,执行速度因此加快.不同的Shell内置命令有所不同. A.2.1  bash内置命令 .:执行当前进程环境中的程序.同source. . file:dot命令从文件file中读取命令并执行. : 空操作,返回退出状态0. alias:显示和创建已有命令的别名. bg:把作业放到后台. bin