Created
May 21, 2012 15:25
-
-
Save asselin/2762904 to your computer and use it in GitHub Desktop.
Handlebars block helper to extend if to support generic Javascript logical expressions
This file contains hidden or 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
| {{#jsif "astring=='abc'"}} | |
| Is true | |
| {{else}} | |
| Is false | |
| {{/jsif}} |
This file contains hidden or 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
| This function adds support to Handlebars for evaluating generic javascript expressions (i.e. what if SHOULD have been). |
This file contains hidden or 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('jsif', function(expr, options) { | |
| if (arguments.length < 2) | |
| throw new Error("Handlerbars Helper 'jsif' needs a parameter"); | |
| with (this) {var result = eval(expr)}; | |
| if (result) { | |
| return options.fn(this); | |
| } else { | |
| return options.inverse(this); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment