从自定义helper调用Handlebars if helper

Calling Handlebars if helper from a custom helper

本文关键字:helper if Handlebars 调用 自定义      更新时间:2023-09-26

我正在寻找一种方法来覆盖车把中的"if"帮助器来进行预处理,但随后调用原始的if帮助器来完成其余的繁重工作。

如果我这样做,它会建立一个无限循环:

// objectCreate is just a function that's supposed to clone an object.
Handlebars._helpers = objectCreate(Handlebars.helpers);
Handlebars.registerHelper('if', function(conditional, options){
        console.log("if helper");
        conditional = false;
        Handlebars._helpers["if"].call(this, conditional, options)

    }); 

有人曾经这样做过吗?我知道我可以做一个自定义的助手,如"conIf",然后调用原来的,但我想保持相同的名称,如果可能的话。

谢谢你的帮助!

终于找到了:

Handlebars._helpers = {};
// This is what somehow breaks the pointer and stops it from forever looping. 
for(var i in Handlebars.helpers){
    Handlebars._helpers[i] = Handlebars.helpers[i];
}

Handlebars.registerHelper('if', function(conditional, options){
    return Handlebars._helpers["if"].call(Handlebars, conditional, options);
});