[JavaScript] Use template engine in Dojo
This is a trial to embed the underscore template utility into Dojo toolkit.
define("myjs/Header",["dojo/_base/declare","dijit/_WidgetBase","dojo/text!./templates/header.html"], function(declare, _WidgetBase,tpl){ return declare(‘myjs.Header‘,[_WidgetBase], { render: function(context){ this.postMixInProperties(); var compiledTmpl = _.template(tpl, { citys : ["BeiJing","ShangHai","ShenZhen"] }); this.srcNodeRef.innerHTML = compiledTmpl; }, buildRendering: function(){ this.render(); }, postMixInProperties: function(){ this.inherited(arguments); } }); });
<ul> <% for (var i = 0; i < citys.length; i++) { %> <% var city= citys[i]; %> <li> <em><%= city %></em> </li> <% } %> </ul>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Dojo Application</title> <link rel="stylesheet" href="myjs/resources/main.css"> <script src="http://underscorejs.org/underscore-min.js"></script> </head> <body class="claro"> <div id="page"> <div id="header"> <div id="nav"></div> </div> <div id="main"></div> <div id="footer"></div> </div> <script src="dojoConfig.js"></script> <script src="dojo/dojo.js"></script> <script type="text/javascript"> if(require){ require(["myjs/Header","dojo/domReady!"],function(Header){ var options = {}; var header = new Header(options,"nav"); header.startup(); }); } </script> </body> </html>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。