handlebarhelpers.js档案
var $ = require('jquery'),
Handlebars = require('handlebars'),
Backbone = require('backbone');
module.exports.ifCondhelper = function (Handlebars, options) {
Handlebars.registerHelper('ifequal', function(value1, value2, options) {
if(value1 === value2) {
return options.fn(this);
}
return options.inverse(this);
});
}
模板
{{#ifCondhelper.ifequal type ../../notificationMessage.SkuMappingNotAvailableMessageP1}}
<li notid="{{id}}"><a href="#">{{../../../notificationMessage.SkuMappingNotAvailableMessageP1}}: Something Something {{../../../notificationMessage.SkuMappingNotAvailableMessageP2}}: Something Something</a> </li>
{{/ifCondhelper.ifequal}}
我正在使用Browserify和Backbone如何在browserify中全局访问帮助程序?请帮忙
答案 0 :(得分:1)
您只需要使用registerHelper
声明它,您实际上无法将其作为单独的模块进行声明
// helpers.js
var Handlebars = require('handlebars');
Handlebars.registerHelper('ifequal', function(value1, value2, options) {
if(value1 === value2) {
return options.fn(this);
}
return options.inverse(this);
});
module.exports = {} // This line not needed, just wanted to show you export nothing
现在在你的把手中编译代码中的某个地方
// Just including this module with require will
// register the helpers to the Handlebars global object
require('./helpers.js');
// Now the rest of your code
Handlebars = require('handlebars');
var template = Handlebars.compile(...);
...
...
差不多就是这样。
在您的模板中,您可以像这样使用它:
{{#ifequal type ../../notificationMessage.SkuMappingNotAvailableMessageP1}}
<li notid="{{id}}">
<a href="#">
{{../../../notificationMessage.SkuMappingNotAvailableMessageP1}}: Something Something {{../../../notificationMessage.SkuMappingNotAvailableMessageP2}}: Something Something</a>
</li>
{{/ifequal}}