How to compile jade templates in to JavaScript functions to use them on client side?

后端 未结 6 952
清歌不尽
清歌不尽 2021-01-04 11:31

I want to use compiled jade templates on client side. How should I compile them to get javascript files ? https://github.com/visionmedia/jade

6条回答
  •  [愿得一人]
    2021-01-04 12:12

    Yes you can! https://github.com/techpines/asset-rack#jadeasset

    I just open sourced "asset-rack", a nodejs project that can can precompile jade templates and serve 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.

    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());
    

提交回复
热议问题