Express Handlebars 'partial' helper

烈酒焚心 提交于 2019-12-23 06:07:03

问题


I want to implement something like this https://stackoverflow.com/a/18431716/1563880 with this lib

Here is my code:

var expressHandlebars  = require('express-handlebars');
var Handlebars = expressHandlebars.create({
    ....
    helpers: {
        partial: function(partialName, context, hash) {
            return Handlebars.getPartials()
                 .then(function(partials){
                     return partials[partialName](context, hash);
                 });
        }
    }
})

and use it in this way:

{{{partial partialName this}}} <!-- partialName is a variable -->

but result is [object Object] and this object is Promise instance.

How can I get template content?


回答1:


Final code should be like this:

var expressHandlebars  = require('express-handlebars');
var Handlebars = expressHandlebars.create({
    ....
    helpers: {
        partial: function(partialName, context, hash) {
            return Handlebars.partials[partialName](context, hash); //changes
        }
    },
    ....
});

Handlebars.getPartials()
    .then(function(partials){
        Handlebars.partials = partials;
    });

Please, correct me if I wrong



来源:https://stackoverflow.com/questions/27820414/express-handlebars-partial-helper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!