Using Jade templates in Backbone.js

后端 未结 5 721
南旧
南旧 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:46

    I just open sourced a nodejs project, called "asset-rack", that can can precompile jade templates and offer them in the browser as javascript functions.

    This means that rendering is blazingly fast, even faster then micro-templates because there is no compilation step in the browser.

    The architecture is a little different then what you mention, just one js file for all templates called "templates.js" or whatever you want.

    You can check it out here, https://github.com/techpines/asset-rack#jadeasset

    First you set it up on the server as follows:

    new JadeAsset({
        url: '/templates.js',
        dirname: __dirname + '/templates'
    });
    

    If you template directory looked like this:

    templates/
      navbar.jade
      user.jade
      footer.jade
    

    Then all your templates come into the browser under the variable "Templates":

    $('body').append(Templates.navbar());
    $('body').append(Templates.user({name: 'mike', occupation: 'sailor'});
    $('body').append(Templates.footer());
    

    Anyway, hope that helps.

提交回复
热议问题