How to define jade template for using with Backbon

2019-05-10 05:49发布

问题:

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 :)