Embedding an ejs template inside of an erb template

后端 未结 3 1425
天涯浪人
天涯浪人 2021-02-10 06:48

I\'m building a javascript-heavy rails 3 app. It uses underscore.js, which has a very elegant templating mechanism built on top of ejs ( http://embeddedjs.com/).

The pr

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-10 07:42

    I use this trick to solve the problem:

    // Using custom tags to be able to use regular for templates in templates
    var ejs = require('ejs');
    ejs.open = '{{';
    ejs.close = '}}';
    
    // Using html extension for custom ejs tags
    app.register('.html', ejs);
    
    app.set('views', __dirname + '/views');
    app.set('view engine', 'html');
    

    This changes <% %> to {{ }}, and let me use <% %> for templates which are used by JS. This works for me as I don't have classic style templates (<% %>).

    If you have a lot of those you may want to do the same trick but for underscore.js templates.

提交回复
热议问题