如何在汇编0.17.1中注册自定义手把助手 [英] How to Register custom handelbars helper in assemble 0.17.1

查看:92
本文介绍了如何在汇编0.17.1中注册自定义手把助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的assemblefile.js文件中,我尝试注册一个自定义帮助器.辅助程序本身确实可以工作,因为我在带有汇编的grunt项目中使用了它.

In my assemblefile.js I try to register a custom helper. The helper itself does work since i have it in use in a grunt project with assemble.

assemble: {
  options: {
    helpers: ['./src/helper/custom-helper.js' ]
  }
}

在汇编0.17.1中,我像这样尝试过,但是它不起作用.有人知道该怎么做吗?

In assemble 0.17.1 I tried it like this but it doesn´t work. Does anyone know how to do this?

app.helpers('./src/helper/custom-helper.js');

custom-helper.js:

custom-helper.js:

module.exports.register = function (Handlebars, options, params)  {

    Handlebars.registerHelper('section', function(name, options) {
        if (!this.sections) {
        this.sections = {};
    }
    this.sections[name] = options.fn(this);
    return null;;
    });

};

推荐答案

assemble现在是在templates模块的顶部构建的,因此您可以使用.helper.helpers方法在assemble中注册助手,这会将它们注册到Handlebars. 此链接包含有关注册助手的更多信息.

assemble is built on top of the templates module now, so you can use the .helper and .helpers methods for registering helpers with assemble, which will register them with Handlebars. This link has more information on registering the helpers.

由于使用了templates api,因此在示例中不必使用.register方法包装帮助程序.您可以只导出helper函数,然后在使用汇编进行注册时将其命名,如下所示:

Since the templates api is used, you don't have to wrap the helpers with the .register method in your example. You can just export the helper function, then name it when registering with assemble like this:

// custom-helper.js
module.exports = function(name, options) {
  if (!this.sections) {
    this.sections = {};
  }
  this.sections[name] = options.fn(this);
  return null;
};

// register with assemble
var app = assemble();
app.helper('section', require('./custom-helper.js'));

您还可以导出带有辅助对象的对象,并使用.helpers方法一次注册所有对象:

You may also export an object with helpers and register them all at once using the .helpers method:

// my-helpers.js
module.exports = {
  foo: function(str) { return 'FOO: ' + str; },
  bar: function(str) { return 'BAR: ' + str; },
  baz: function(str) { return 'BAZ: ' + str; }
};

// register with assemble
var app = assemble();
app.helpers(require('./my-helpers.js'));

使用.helpers方法注册对象时,属性键用于辅助名称

When registering the object with the .helpers method, the property keys are used for the helper names

这篇关于如何在汇编0.17.1中注册自定义手把助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆