doT.js 模板引擎的使用

來源:互聯網
上載者:User

標籤:

dot.js是一個模板架構,在web前端使用。

dot.js作為模板引擎,

主要的用途就是,在寫好的模板上,放進資料,產生含有資料的html代碼。

這是很簡單的web前端模板架構,

簡單說幾個東西,你就會用了!

1、在模板中,it這個變數,就是傳遞進去的資料對象,應該就是英語的items;

2、doT.template( template )( obj )  代碼解釋:把模板template,作為參數傳入doT.template()  方法,dot就把模板處理一下,然後你再把資料對象傳入,傳回值,就是html與資料 一起產生的html代碼了,再拼接到頁面即可;

3、在模板中,{{}}  包裹的內容,就是javascript代碼,dot會負責處理,因為這寫法怪異,新手一般出現模板寫錯的情況,仔細排查即可 。

<!DOCTYPE html><html>    <head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title>無標題文檔</title></head>    <body>    <ul id=‘showInfo‘>      <script type="text/template" charset="utf-8" id=‘template_1‘>

  {{if (it && it.length > 0) {}} {{for (var i = 0; i < it.length; i++) {}} < li > {{ = it[i].name}} < /li>{{ } }}{{
}
else{
}}
<li>沒有資料。</li > {{ } }}</script> </ul> <br> <hr> <br> <ul id=‘showInfo2‘></ul> </body> <script type="text/javascript" charset="utf-8" src=‘doT.min.js‘></script> <script type="text/javascript" charset="utf-8">var template = document.getElementById(‘template_1‘).innerHTML; var obj = [{ name: ‘d1‘ }, { name: ‘d2‘ }, { name: ‘d3‘ }]; //傳遞一個有資料的數組進去 document.getElementById(‘showInfo‘).innerHTML = doT.template(template)(obj); //傳入的資料為空白 document.getElementById(‘showInfo2‘).innerHTML = doT.template(template)([]);</script></html>

 

doT.js 模板引擎的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.