Using Jade templates in Backbone.js

后端 未结 5 735
南旧
南旧 2020-12-24 07:07

I love the HAML-like syntax of Jade\'s templating engine in Node.js, and I would love to use it client-side within Backbone.js.

I\'ve seen Backbone commonly using Un

5条回答
  •  清酒与你
    2020-12-24 07:49

    I was able to run Jade client-side using jade-browser project.

    Integration with Backbone is then simple: Instead of _template() I'm using jade.compile().

    HTML (scripts and template):

    
    
    
    
    
    
    
    

    JavaScript (integration with Backbone.View):

    var jade = require("jade");
    
    var TestView = Backbone.View.extend({
    
      initialize: function() {
        this.template = jade.compile($("#test").text());
      },
    
      render: function() {
        var html = this.template({ item: 'hello, world'});
        $('body').append(html);
      }
    });
    
    var test = new TestView();
    test.render();
    

    HERE is the code.

提交回复
热议问题