How to change the default delimiter of Handlebars.js?

前端 未结 8 1868
半阙折子戏
半阙折子戏 2020-12-10 02:41

I need to use handlebars.js and I also use Blade template engine from Laravel (PHP Framework). The tags {{}} conflict with blade\'s placeholders that are exactly the same.

8条回答
  •  无人及你
    2020-12-10 02:46

    I created handlebars-delimiters on GitHub / npm to make it easy to use custom delims with Handlebars.

    var Handlebars = require('handlebars');
    var useDelims = require('handlebars-delimiters');
    
    var a = Handlebars.compile('{{ name }}<%= name %>')({name: 'Jon'});
    console.log(a);
    //=> 'Jon<%= name %>'
    
    
    // Pass your instance of Handlebars and define custom delimiters
    useDelims(Handlebars, ['<%=', '%>']);
    var b = Handlebars.compile('{{ name }}<%= name %>')({name: 'Jon'});
    console.log(b);
    //=> '{{ name }}Jon'
    

    The idea for the compile function came from https://stackoverflow.com/a/19181804/1267639

    Suggestions for improvement or pull requests are welcome!

提交回复
热议问题