注册Custom Helper Express和HBS

时间:2016-01-06 08:53:30

标签: javascript node.js express handlebars.js

我是一个完整的Node菜鸟,所以请耐心等待。

我使用带有-hbs标志的快速生成器模块将默认模板引擎切换为把手。

我现在正在尝试注册一个自定义帮助程序,以允许我将特定于页面的内容添加到位于布局模板中的元素。

我无法找到我应该在快递中注册自定义hbs助手的位置: 我用以下代码(我在这里找到http://www.apkapps.link/questions/2420017/handlebars-with-express-different-html-head-for-different-pages)尝试了“。\ node_modules \ handlebars \ dist \ cjs \ handlebars \ compiler \ helpers.js”:

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

exports.section = section;

但是当我尝试从模板中调用助手时,我得到一个“失踪帮手:'部分'”

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:5)

从Handlebars的文档中,您必须将您的助手注册为

var hbs = require('hbs');

hbs.registerHelper('helper_name', function(...) { ... });
hbs.registerPartial('partial_name', 'partial value');

您无需修改​​“。\ node_modules \ handlebars \ dist \ cjs \ handlebars \ compiler \ helpers.js

相关问题