Last active
November 17, 2017 20:58
-
-
Save LiamKarlMitchell/23a1caa4a77b1a68487379e4117447b0 to your computer and use it in GitHub Desktop.
Helpful Handlebars template 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
// Show an HTML encoded checkbox for the result of a boolean like value. | |
Handlebars.registerHelper('checkbox', function (obj) { | |
return new Handlebars.SafeString(obj==true ? '☑' : '☐') | |
}) |
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
// Returns HTML JSON dump in a pre tag. | |
Handlebars.registerHelper('dump', function (obj) { | |
return new Handlebars.SafeString("<pre>"+JSON.stringify(obj, true, 2)+"</pre>") | |
}) |
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
// 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 | |
} | |
}); |
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
// 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