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); } });});
<% for (var i = 0; i < citys.length; i++) { %> <% var city= citys[i]; %>
- <%= city %>
<% } %>
Dojo Application
<script src="http://underscorejs.org/underscore-min.js"></script> <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>