1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- /* handlebars helpers */
- // moment syntax example: moment(Date("2011-07-18T15:50:52")).format("MMMM YYYY")
- // usage: {{dateFormat creation_date format="MMMM YYYY"}}
- Handlebars.registerHelper('dateFormat', function(context, block) {
- if (window.moment) {
- var f = block.hash.format || "MMM DD, YYYY hh:mm:ss A";
- return moment(context).format(f); //had to remove Date(context)
- } else {
- return context; // moment plugin not available. return data as is.
- }
- });
- // extended "if" block helper
- // usage {{#ifCond var1 '==' var2}}
- Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
- 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);
- case '||':
- return (v1 || v2) ? options.fn(this) : options.inverse(this);
- default:
- return options.inverse(this);
- }
- });
- /**
- * The {{#exists}} helper checks if a variable is defined.
- */
- Handlebars.registerHelper('exists', function(variable, options) {
- if (typeof variable !== 'undefined') {
- return options.fn(this);
- } else {
- return options.inverse(this);
- }
- });
|