Skip to content

Instantly share code, notes, and snippets.

@niveshsaharan
Created September 11, 2019 10:12
Show Gist options
  • Save niveshsaharan/d9ba50c497c2506d91c19e7c2899fb22 to your computer and use it in GitHub Desktop.
Save niveshsaharan/d9ba50c497c2506d91c19e7c2899fb22 to your computer and use it in GitHub Desktop.
Handlebars custom helpers
/**
* eq helper to check if the given values are equal
*
* {{#eq 4 4}} The values are equal {{/eq}}
*/
Handlebars.registerHelper('eq', function(a, b) {
const next = arguments[arguments.length - 1];
return a === b ? next.fn(this) : next.inverse(this);
});
/**
* ge helper to check if the value is greater than or equal to
*
* {{#ge 5 4}} The value is greater {{/ge}}
*/
Handlebars.registerHelper('ge', function(a, b) {
const next = arguments[arguments.length - 1];
return a >= b ? next.fn(this) : next.inverse(this);
});
/**
* gt helper to check if the value is greater
*
* {{#gt 5 4}} The value is greater {{/gt}}
*/
Handlebars.registerHelper('gt', function(a, b) {
const next = arguments[arguments.length - 1];
return a > b ? next.fn(this) : next.inverse(this);
});
/**
* Helper to check if the value is lower or equal
*
* {{#le 5 9}} The value is lower or equal {{/le}}
*/
Handlebars.registerHelper('le', function(a, b) {
const next = arguments[arguments.length - 1];
return a <= b ? next.fn(this) : next.inverse(this);
});
/**
* Helper to check if the value is less than
*
* {{#lt 5 9}} The value is lower {{/lt}}
*/
Handlebars.registerHelper('lt', function(a, b) {
const next = arguments[arguments.length - 1];
return a < b ? next.fn(this) : next.inverse(this);
});
/**
* Helper to check if the value are not equal
*
* {{#ne 5 9}} The value are unqual {{/ne}}
*/
Handlebars.registerHelper('ne', function(a, b) {
const next = arguments[arguments.length - 1];
return a !== b ? next.fn(this) : next.inverse(this);
});
/**
* Helper for basic math operations
*
* {{#math 5 "+" 9}}
*/
Handlebars.registerHelper('math', (lvalue, operator, rvalue, options) => {
lvalue = parseFloat(lvalue);
rvalue = parseFloat(rvalue);
return {
'+': lvalue + rvalue,
'-': lvalue - rvalue,
'*': lvalue * rvalue,
'/': lvalue / rvalue,
'%': lvalue % rvalue,
}[operator];
});
/**
* Helper to check if the string has part
*
* {{#contains "test" "This is a test message"}} Yes :) {{/contains}}
*/
Handlebars.registerHelper('contains', function(needle, haystack, options) {
needle = Handlebars.escapeExpression(needle);
haystack = Handlebars.escapeExpression(haystack);
return haystack.indexOf(needle) > -1 ? options.fn(this) : options.inverse(this);
});
/**
* Helper to call a custom javascript function. The must must be a global function.
*
* {{fn "Math.round" 4.5}}
*/
Handlebars.registerHelper('fn', function(fn) {
const args = [];
for (let i = 1; i < arguments.length - 1; i++) {
args.push(arguments[i]);
}
let obj = window;
fn.split('.').forEach(fnName => {
obj = typeof obj === 'object' && obj ? obj[fnName] : fnName;
});
return typeof obj === 'function' ? obj(...args) : null;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment