From 3b6b6602392ece3aa7ba8b657c453a4024d99a7f Mon Sep 17 00:00:00 2001 From: Darryl Date: Sun, 8 Dec 2013 00:13:36 +0000 Subject: [PATCH] Update helpers.js + Added formatDate It would need momentjs included, but in its absence it returns the context. + Minor code syntax corrections that JSLint noticed. --- src/helpers.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 02c1cef..1bcf461 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -10,7 +10,7 @@ var isArray = function(value) { return Object.prototype.toString.call(value) === '[object Array]'; - } + }; var ExpressionRegistry = function() { this.expressions = []; @@ -28,7 +28,7 @@ return this.expressions[operator](left, right); }; - var eR = new ExpressionRegistry; + var eR = new ExpressionRegistry(); eR.add('not', function(left, right) { return left != right; }); @@ -59,10 +59,10 @@ var isHelper = function() { var args = arguments - , left = args[0] - , operator = args[1] - , right = args[2] - , options = args[3] + left = args[0], + operator = args[1], + right = args[2], + options = args[3] ; if (args.length == 2) { @@ -104,6 +104,18 @@ )); }); + // format an ISO date using Moment.js + // http://momentjs.com/ + // usage: {{dateFormat creation_date format="MMMM YYYY"}} + Handlebars.registerHelper("formatDate", function(context, block) { + if (window.moment && context && moment(context).isValid()) { + var f = block.hash.format || "MMM Mo, YYYY"; + return moment(Date(context)).format(f); + } else { + return context; // moment plugin not available. return data as is. + } + }); + return eR; -})); \ No newline at end of file +}));