【问题标题】:Is there a way to integrate handlebars-helpers with hapi?有没有办法将车把助手与 hapi 集成?
【发布时间】:2015-10-06 11:26:22
【问题描述】:

我想将handlebars-helpers 节点模块与我的车把模板一起使用。我使用hapi 作为支持车把的框架。我还没有找到任何文档或示例来说明如何将车把助手与 hapi 一起使用,并使用车把作为视图引擎。

有可能吗?如果可以,解决办法是什么?

【问题讨论】:

    标签: handlebars.js helpers view-helpers hapijs


    【解决方案1】:

    启动hapi服务器后

    // hapi v17
    try {
        await server.start();
    } catch (err) {
        throw err;
    }
    
    //......
    
    // add some handlebars helpers    
    let hbs = server.realm.plugins.vision.manager._engines.hbs;
    // console.log('handlebars_helpers', handlebars_helpers);
    if (handlebars_helpers) {
      for (let key in handlebars_helpers) {
        if (key) {
          // console.log('key', key, helpers[key]);
          hbs.module.helpers[key] = handlebars_helpers[key];
        }
      }
    }
    //check your helper is registered
    // console.log('hbs.module.helpers', hbs.module.helpers);
    // add some handlebars helpers
    

    【讨论】:

      【解决方案2】:

      根据 hapijs api docs for views,我认为目前不可能:

      http://hapijs.com/api#serverviewsoptions

      helpersPath - 助手所在的目录路径。助手是模板中使用的函数,用于使用模板上下文或其他输入执行转换和其他数据操作。 helpers 目录中的每个 '.js' 文件都被加载,文件名用作 helper 名称。文件必须使用签名函数(上下文)导出单个方法并返回字符串。不支持子文件夹并被忽略。默认不支持助手(空路径)。请注意,jade 不支持以这种方式加载帮助程序。

      看起来handlebars-helpers 的签名与hapi 所需的签名不同

      【讨论】:

      • 这是我得出的结论。我希望我错了。 ;-)
      猜你喜欢
      • 1970-01-01
      • 2020-01-03
      • 2020-04-02
      • 2020-07-16
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多