Giter Site home page Giter Site logo

dot.js's Introduction

js模板引擎:doT.js

doT上手比较容易,但之前熟悉了mustache,改用doT时有点不习惯,但用两次就好了。

下载doT.js(里面有个doU.js不要用,doU.js是为了测试遗留问题的)。举个简单的使用例子:

##HTML部分: <script id="j-tmpl" type="text/template"> {{ if(it.success){ }}

results:

    {{ for (var i = 0, l = it.data.length; i < l; i++) { }}
  • {{=it.data[i].title}}

    {{!it.data[i].message}}

  • {{ } }}
      {{ }else{ }}

      暂无数据

      {{ } }} </script> ##JS部分: <script> var obj = { success: true, data:[ {title:'item1',message:11}, {title:'item1',message:22} ] } var tmpl = document.getElementById('j-tmpl').innerHTML; var doTtmpl = doT.template(tmpl); console.log(doTtmpl(obj )); </script>

      看了例子,就应该会使用了。 {{=it.xx}} 取obj.xx的值 {{ }} 里面放if els / for 等表达式 {{!it.xx}} 取把obj.xx转义后的值 这些基本够用了,还有复杂的应用,可以看doT主页内的examples、docs。 并且,可以很容易把doT写成jquery插件: <script> $.extend({ tmpl: function(template, data){ return doT.template(template).apply(null,[data]); } }); </script>

dot.js's People

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.