doT.js

Posted A&B

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了doT.js相关的知识,希望对你有一定的参考价值。

最近用到的数据模板引擎有很多,今天讲的doT.js也是其中一种。

doT.js的特点是体积小,速度快,并且不依赖其他插件。

官网下载:http://olado.github.io

下面是用法:

模板引擎

<script id="visitlisttmpl" type="text/x-dot-template">
    {{if( it && it.length>0 ){ }}
    {{ for(var i=0; i< it.length; i++) { }}
        
    {{ } }}

    {{ }else{ }}

    {{ } }}
 </script>

调用模板引擎

<script type="text/javascript">
    var evalText = doT.template($("#visitlisttmpl").text());//visitlisttmpl是模板
    html = evalText(data);//data是已经得到的数据集合,json数据格式
  $("#temp").html(html);
</script>

 

 在使用模板引擎的过程中,会遇到一些问题,比如说是if...else...的用法,如下:

{? it[i].data === null }}//if

{{?? }}//else

{{? }}//特别是最后的这个,一定不能忘

当然还有一些其他的用法,以后在使用的过程中会慢慢补充上。


以上是关于doT.js的主要内容,如果未能解决你的问题,请参考以下文章

doT.js源码解读

doT.js——前端javascript模板引擎问题备忘录

dot.js

doT.js 模板引擎

dot.js的使用

doT.js