javascript - 注册 Custom Helper Express 和 HBS

标签 javascript node.js express handlebars.js

我是一个完全的 Node 菜鸟,所以请多多包涵。

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

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

我找不到应该在 express 中注册自定义 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;

但是当我尝试从模板调用助手时,我得到一个“缺少助手:'section'”

谁能指出我正确的方向?

最佳答案

根据 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

关于javascript - 注册 Custom Helper Express 和 HBS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34628978/

相关文章:

javascript - Google OAuth2 出现问题(应用没有后端,因此只有客户端)

javascript - 使用 PHP 列出来自使用 SOAP 的 Web 服务的信息

node.js - 使用快速发布方法在数据库中插入新数据

node.js - 对 http 动词的 Mongoose/Express 授权

node.js - 如何获取 session 的过期时间戳?

javascript - 将回调值返回给主函数

javascript - 使用 mongoose populate 过滤数据

javascript - 使用 bcrypt 进行密码散列

javascript - 在 Node.js 中模拟嵌套模块?

javascript - Protractor - 将 JavaScript 注入(inject)测试页面