Skip to content

Instantly share code, notes, and snippets.

@red2678
Last active August 29, 2015 14:07
Show Gist options
  • Save red2678/c31c58a35b6075a152d5 to your computer and use it in GitHub Desktop.
Save red2678/c31c58a35b6075a152d5 to your computer and use it in GitHub Desktop.
Handlebars Helper If
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
console.log(v1)
switch (operator) {
case '==':
return (v1 == v2) ? options.fn(this) : options.inverse(this);
case '===':
return (v1 === v2) ? options.fn(this) : options.inverse(this);
case '<':
return (v1 < v2) ? options.fn(this) : options.inverse(this);
case '<=':
return (v1 <= v2) ? options.fn(this) : options.inverse(this);
case '>':
return (v1 > v2) ? options.fn(this) : options.inverse(this);
case '>=':
return (v1 >= v2) ? options.fn(this) : options.inverse(this);
case '&&':
return (v1 && v2) ? options.fn(this) : options.inverse(this);
case '||':
return (v1 || v2) ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
});
{{#ifCond @key '<' 11}}do something{{else}}do something else{{/ifCond}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment