Express Handlebars' partial'帮手

时间:2015-01-07 13:18:00

标签: javascript express handlebars.js

我想实现这样的东西 https://stackoverflow.com/a/18431716/1563880 this lib

这是我的代码:

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);
                 });
        }
    }
})

并以这种方式使用它:

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

但结果是 [object Object] 这个对象是Promise实例。

如何获取模板内容?

1 个答案:

答案 0 :(得分:0)

最终代码应该是这样的:

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;
    });

如果我错了,请纠正我