backbone.js - How to register a global handlebar helper -
handlebarhelpers.js file
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); }); }
template
{{#ifcondhelper.ifequal type ../../notificationmessage.skumappingnotavailablemessagep1}} <li notid="{{id}}"><a href="#">{{../../../notificationmessage.skumappingnotavailablemessagep1}}: something {{../../../notificationmessage.skumappingnotavailablemessagep2}}: something</a> </li> {{/ifcondhelper.ifequal}}
i using browserify , backbone how access helper globally in browserify? please help
you need declare registerhelper
, can't separate module
// 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 = {} // line not needed, wanted show export nothing
and in handlebars compile function somewhere in code
// including module require // register helpers handlebars global object require('./helpers.js'); // rest of code handlebars = require('handlebars'); var template = handlebars.compile(...); ... ...
pretty that's it.
and in template use :
{{#ifequal type ../../notificationmessage.skumappingnotavailablemessagep1}} <li notid="{{id}}"> <a href="#"> {{../../../notificationmessage.skumappingnotavailablemessagep1}}: {{../../../notificationmessage.skumappingnotavailablemessagep2}}: something</a> </li> {{/ifequal}}
Comments
Post a Comment