Skip to content

Instantly share code, notes, and snippets.

@LiamKarlMitchell
Last active November 17, 2017 20:58
Show Gist options
  • Save LiamKarlMitchell/23a1caa4a77b1a68487379e4117447b0 to your computer and use it in GitHub Desktop.
Save LiamKarlMitchell/23a1caa4a77b1a68487379e4117447b0 to your computer and use it in GitHub Desktop.
Helpful Handlebars template helpers.
// Show an HTML encoded checkbox for the result of a boolean like value.
Handlebars.registerHelper('checkbox', function (obj) {
return new Handlebars.SafeString(obj==true ? '☑' : '☐')
})
// Returns HTML JSON dump in a pre tag.
Handlebars.registerHelper('dump', function (obj) {
return new Handlebars.SafeString("<pre>"+JSON.stringify(obj, true, 2)+"</pre>")
})
// Returns the length of the array, length property or the number of object keys on the object.
Handlebars.registerHelper('length', function (obj) {
if (Array.isArray(obj)) {
return obj.length
} else if (obj.hasOwnProperty('length')) {
return obj.length
} else {
return Object.keys(obj).length
}
});
// To Hex with pad left with 0.
Handlebars.registerHelper('toHex', function (obj, zeroPadCount) {
if (zeroPadCount===undefined) {
zeroPadCount = 2
}
return parseInt(obj, 10).toString(16).toUpperCase().padStart(zeroPadCount, '0')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment