Rendering templates within helpers in handlebars

前端 未结 2 1793
不思量自难忘°
不思量自难忘° 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: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));
    });
    

提交回复
热议问题