我是一个完全的 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/