Last active
August 29, 2015 14:14
-
-
Save jeff-hager-dev/409bf41a9ec43bb53f57 to your computer and use it in GitHub Desktop.
A simple handlebars helper file to use with NodeJs/Express that is using the handlebars for the view engine.
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
var moment = require('moment'); | |
var _dateFormats = { | |
short: "MMMM DD, YYYY", | |
long: "dddd DD.MM.YYYY HH:mm" | |
}; | |
var barsHlprs = {}; | |
barsHlprs.ifIn = function (elem, list, options) { | |
if (list.indexOf(elem) > -1) { | |
return options.fn(this); | |
} | |
return options.inverse(this); | |
}; | |
barsHlprs.block = function (name) { | |
var blocks = this._blocks; | |
content = blocks && blocks[name]; | |
return content ? content.join('\n') : null; | |
}; | |
barsHlprs.contentFor = function (name, options) { | |
var blocks = this._blocks || (this._blocks = {}); | |
block = blocks[name] || (blocks[name] = []); //Changed this to [] instead of {} | |
block.push(options.fn(this)); | |
}; | |
barsHlprs.dateFormatter = function (datetime, format) { | |
format = _dateFormats[format] || format; | |
return moment(datetime).format(format); | |
}; | |
barsHlprs.lenghtOf = function (arr) { | |
if (arr && arr.length) { | |
return arr.length; | |
} | |
return 0; | |
} | |
barsHlprs.hasPastDate = function (date, options) { | |
if (date) { | |
if (moment().diff(moment(date)) > 0) { | |
return options.fn(this); | |
} | |
else { | |
return options.inverse(this); | |
} | |
} | |
else { | |
return options.inverse(this); | |
} | |
}; | |
module.exports = barsHlprs; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment