Created
June 18, 2015 19:42
-
-
Save dperrymorrow/0b3cbfae93499515bc97 to your computer and use it in GitHub Desktop.
Handlebars Helpers
This file contains 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
(function () { | |
"use strict"; | |
Handlebars.registerHelper('isSelected', function (optId, id) { | |
return optId === id ? 'selected="true"' : ''; | |
}); | |
Handlebars.registerHelper('classIf', function (val, className) { | |
return val ? className : ''; | |
}); | |
Handlebars.registerHelper('truncate', function (str, len) { | |
if (str.length > len && str.length > 0) { | |
var new_str = str + " "; | |
new_str = str.substr(0, len); | |
new_str = str.substr(0, new_str.lastIndexOf(" ")); | |
new_str = (new_str.length > 0) ? new_str : str.substr(0, len); | |
return new Handlebars.SafeString(new_str + '...'); | |
} | |
return str; | |
}); | |
Handlebars.registerHelper('round', function (num) { | |
return parseInt(num, null); | |
}); | |
Handlebars.registerHelper('isZero', function (str) { | |
return Handlebars.helpers.round(str) === 0; | |
}); | |
Handlebars.registerHelper('isEmpty', function (str) { | |
return str.replace(" ") === ""; | |
}); | |
Handlebars.registerHelper('blankForZero', function (str) { | |
var val = Handlebars.helpers.round(str); | |
return val === 0 ? '' : val; | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment