Rendering templates within helpers in handlebars

前端 未结 2 1794
不思量自难忘°
不思量自难忘° 2021-02-14 15:10

Because there seem to be no answer on this: Passing variables through handlebars partial yet, I\'m currently working on a little workaround to get this work. So, the idea is to

相关标签:
2条回答
  • 2021-02-14 15:28

    I'd just like to point out that using triple brackets eliminates the need to run any additional methods to convert to HTML. For example, when accessing the template data just use triple curly braces {{{templateData}}}, which allows you to get raw HTML.

    0 讨论(0)
  • 2021-02-14 15:40

    From Handlebars doc :

    Handlebars will not escape a Handlebars.SafeString. If you write a helper that generates its own HTML, you will usually want to return a new Handlebars.SafeString(result). In such a circumstance, you will want to manually escape parameters.

    Try

    hbs.registerHelper(name, function (args) {
        args = args || {};
        var template = hbs.compile(fs.readFileSync(__dirname + '/' + file, 'utf8'));
    
        // return new hbs.SafeString(template(args));
        // From @Maroshii 
        // the SafeString method must be accessed through hbs.handlebars 
        // and not directly through hbs
        // https://github.com/donpark/hbs#handlebars
    
        return new hbs.handlebars.SafeString(template(args));
    });
    
    0 讨论(0)
提交回复
热议问题