【问题标题】:How to debug template in Meteor/handlebars?如何在 Meteor/handlebars 中调试模板?
【发布时间】:2015-08-17 11:23:52
【问题描述】:

根据这个blog post,我应该注册一个帮助程序来更好地调试车把模板,但不起作用:

ReferenceError: Handlebars is not defined

那么,我如何在 Meteor/handlebars 中 {{debug}}

【问题讨论】:

    标签: meteor handlebars.js


    【解决方案1】:

    这是我在自己的项目中用于调试的辅助函数:

    Template.registerHelper("debug", function(optionalValue) { 
      console.log("Current Context");
      console.log("====================");
      console.log(this);
    
      if (optionalValue) {
        console.log("Value"); 
        console.log("===================="); 
        console.log(optionalValue); 
      } 
    });
    

    然后您可以在模板中使用{{debug}} 调用它,它会显示您当前所处的上下文。在http://docs.meteor.com/#/full/template_registerhelper 上阅读更多信息。

    【讨论】:

    • 优秀的 sn-p,不过,我猜 Meteor 推荐使用符号 Template.myTemplate.helpers
    • 是的,但是我相信它只能在那个特定的模板中使用
    • 是的。因此,我更喜欢使用您的答案,但我猜另一种表示法是 正确的
    【解决方案2】:

    在 Meteor 0.4.0 中,您可以像这样注册处理程序:

    Template.myTemplate.helpers({
      helper: function () {
        // some code here
        console.log(arguments);
      }
    });
    

    无需直接调用Handlebars。

    【讨论】:

      【解决方案3】:

      确保您在客户端(或共享)流星代码中注册了您的助手。

      Handlebars.registerHelper('helper', function() {
        // Do stuff
      });
      

      这应该可以通过模板中的{{helper}} 调用。

      【讨论】:

        【解决方案4】:

        为了完整起见:你也可以使用

        Template.registerHelper('helper', helperFunc);
        

        而不是Handlebars.regsterHelper('h',f);

        这样做更好的一个小原因是,如果您决定在某个地方使用其他东西而不是 Handlebars(即 Spacebars,流星适配的真实名称),那么您的应用程序将不需要那么多重构) 喜欢jade for meteor

        这确实是对accepted answer 的评论。期待有一天能达到 50 次。

        【讨论】:

        • Meteor的api变了,现在看来应该和你说的一样了。
        猜你喜欢
        • 2012-05-31
        • 2013-09-05
        • 2013-04-25
        • 1970-01-01
        • 2013-06-10
        • 1970-01-01
        • 2013-10-23
        • 2014-01-20
        • 1970-01-01
        相关资源
        最近更新 更多