Skip to content

Instantly share code, notes, and snippets.

@geranyl
Created January 23, 2014 18:35
Show Gist options
  • Save geranyl/8584241 to your computer and use it in GitHub Desktop.
Save geranyl/8584241 to your computer and use it in GitHub Desktop.
Handlebars custom if else helper
Handlebars.registerHelper('isMobile', function(conditional, options) {
if(conditional)
return options.fn(this)
return options.inverse(this);
});
Example:
Handlebars.registerHelper('isMobile', function(conditional, options) {
var fnTrue=options.fn, fnFalse=options.inverse;
return $('html').hasClass('touch') ? fnTrue(this) : fnFalse(this);
});
Use it as such:
{{#isMobile this}}
<img src="{{image1}}"/>
{{else}}
<img src="{{image2}}"/>
{{/isMobile}}
Passing in "this" allows it to use the same context as the rest of the template.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment