Created
January 23, 2014 18:35
-
-
Save geranyl/8584241 to your computer and use it in GitHub Desktop.
Handlebars custom if else helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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