【问题标题】:Handlebars helper: Compile template and keep @rootHandlebars helper:编译模板并保留@root
【发布时间】:2015-01-26 17:05:16
【问题描述】:

我正在使用 grunt-compile-handlebars 来编译我的把手,但我遇到的问题是,@root 对象在我使用后会发生变化

[...]
handlebars.compile(template);
template(context.data);
[...]

所以当我的 json 对象类似于

{
    "template": "path/to/template",
    "data": {
        "greetings": {...}
    }
 }

这始终是我的@root 对象。

我有类似的东西:

var handlebars = require('../../node_modules/grunt-compile-handlebars/node_modules/handlebars')     ;

module.exports = function(context) {

    var template = handlebars.partials[context.template];

    template = handlebars.compile(template);

    return new handlebars.SafeString( template(context.data) );

};
{{helper .}}

现在我的@root 对象是:

"data": {
    "greetings": {...}
}

有没有办法在调用template(...)时保留@root

注意:这是一个非常简化的示例。我知道我也可以使用{{>path/to/template .}}

【问题讨论】:

    标签: handlebars.js handlebarshelper


    【解决方案1】:

    https://github.com/wycats/handlebars.js/issues/945:

    template(context, {data: {root: options.data.root}})

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-27
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 2014-08-29
      • 1970-01-01
      相关资源
      最近更新 更多