handlebars js返回模板代码以及编译代码

时间:2016-02-24 08:01:49

标签: javascript jquery handlebars.js

我正在使用把手js - 版本4.0.5。

这是我对任何模板引擎的第一手资料。我和jQuery一起使用它。

我创建了一个外部模板文件,并在需要时使用jQuery <div id="dialogViewing" style="display:none;"> <table> <tr> <td> <asp:TextBox ID="lblName" runat="server"></asp:TextBox> <asp:DropDownList ID="ddlLocation" runat="server"> <asp:ListItem>Location 1</asp:ListItem> <asp:ListItem>Location 2</asp:ListItem> <asp:ListItem>Location 3</asp:ListItem> </asp:DropDownList> </td> </tr> </table> </div> 加载模板,如下所示 -

.load()

这是 template.html -

中的模板代码
$("#sidebar-lists").load("/assets/template.html #emp-dep-lists", function(){
   var theTemplateScript = $("#emp-dep-lists").html();
   var theTemplate = Handlebars.compile(theTemplateScript);
   var context = data;
   var theCompiledHtml = theTemplate(context);
   $('ul#sidebar-lists').append(theCompiledHtml);
});

我在页面上使用<span id="emp-dep-lists"> {{#each associateList}} <li> <a href="#"> {{#if image}} <img src="{{image}}" height="35" width="35"> {{else}} <img height="35" width="35" src="/assets/images/black-user-shape.svg"> {{/if}} <span class="title">{{name}}</span> </a> </li> {{/each}} </span> 创建了<ul>。现在,我正在尝试将更多<li>添加到同一个<li>中。虽然一切都变得更顺畅,但它取代了我的第一个<ul>,并且它在第一次出现时添加了模板代码,然后呈现<li>

它给出了以下结果:

<li>

0 个答案:

没有答案