Last active
December 22, 2015 11:09
-
-
Save rob-bar/6463572 to your computer and use it in GitHub Desktop.
IF Handlebars helpers
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_helpers: () -> | |
Handlebars.registerHelper 'ifbool', (b, c) -> | |
if b then do c.fn else do c.inverse | |
Handlebars.registerHelper 'ifequal', (l, r, c) -> | |
if l is r then do c.fn else do c.inverse | |
Handlebars.registerHelper 'unlessequal', (l, r, c) -> | |
unless l is r then do c.fn else do c.inverse | |
Handlebars.registerHelper 'ifcompare', (l, r, o) -> | |
operator = o.hash.operator || "==" | |
operators = | |
'==': (l,r) -> l == r | |
'===': (l,r) -> l is r | |
'!=': (l,r) -> l != r | |
'<': (l,r) -> l < r | |
'>': (l,r) -> l > r | |
'<=': (l,r) -> l <= r | |
'>=': (l,r) -> l >= r | |
'typeof': (l,r) -> typeof l == r | |
if arguments.length < 3 then throw new Error "Handlerbars Helper 'compare' needs 2 parameters" | |
unless operators[operator] then throw new Error "Handlerbars Helper 'compare' doesn't know the operator #{operator}" | |
if operators[operator](l, r) then o.fn @ | |
else o.inverse @ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment