How to define jade template for using with Backbone

家住魔仙堡 提交于 2019-12-07 16:21:15

问题


I need to use template for rendering of each ItemView:

var ItemView = Backbone.View.extend({
    className: 'item',
    template: _.template($('#itemTemplate').html()),

    initialize: function () {

    }
});

So I need to define html template at first:

<script id="itemTemplate" type="text/template">
  <img src="<%= photo %>" alt="<%= name %>" />
  <h1><%= name %><span><%= type %></span></h1>
  <div><%= address %></div>
  <dl>
    <dt>Tel:</dt><dd><%= tel %></dd>
    <dt>Email:</dt><dd><a href="mailto:<%= email %>"><%= email %></a></dd>
  </dl>

But I use Nodejs Jade Template Engine and I don't understand how shuold I define in it. Help please.


回答1:


That's easy, but there's one catch: You don't want Jade to escape attributes content so use foo!='<%= bar &%>' instead of just foo='<%= bar &%>'.

Here we go:

script#itemTemplate(type='text/template')
  img(src!='<%= photo %>', alt!='<%= name %>')
  h1 <%= name %>
    span <%= type %>
  div <%= address %>
  dl
    dt Tel:
    dd <%= tel %>
    dt Email:
    dd
      a(href!='mailto:<%= email %>') <%= email %>

It's tested, so you can use it right away :)



来源:https://stackoverflow.com/questions/12767774/how-to-define-jade-template-for-using-with-backbone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!