Added has_tag helper to coreHelpers, added has_tag unit tests. passing OK

This commit is contained in:
Jorge Niedbalski 2013-11-22 11:19:26 -03:00
parent 216dd75b2c
commit 3782e26516
2 changed files with 45 additions and 0 deletions

View File

@ -453,6 +453,24 @@ coreHelpers.foreach = function (context, options) {
return ret;
};
// ### Check if a tag is contained on the tag list
//
// Usage example:*
//
// `{{#has_tag "test"}} {{tags}} {{else}} no tags {{/has_tag}}`
//
// @param {String} tag name to search on tag list
// @return {String} list of tags formatted according to `tag` helper
//
coreHelpers.has_tag = function (name, options) {
if (_.isArray(this.tags) && !_.isEmpty(this.tags)) {
return (!_.isEmpty(_.filter(this.tags, function (tag) {
return (_.has(tag, "name") && tag.name === name);
}))) ? options.fn(this) : options.inverse(this);
}
return options.inverse(this);
};
// ## Template driven helpers
// Template driven helpers require that their template is loaded before they can be registered.
coreHelpers.paginationTemplate = null;
@ -525,6 +543,8 @@ registerHelpers = function (ghost) {
ghost.registerThemeHelper('helperMissing', coreHelpers.helperMissing);
ghost.registerThemeHelper('has_tag', coreHelpers.has_tag);
ghost.registerAsyncThemeHelper('body_class', coreHelpers.body_class);
ghost.registerAsyncThemeHelper('post_class', coreHelpers.post_class);

View File

@ -555,4 +555,29 @@ describe('Core Helpers', function () {
});
});
describe("has_tag helper", function (done) {
var tags = [{name: 'haunted'}, {name: 'ghost'}];
it('has loaded has_tag helper', function () {
should.exist(handlebars.helpers.has_tag);
});
it('can call function if tag is found', function() {
helpers.has_tag.call({tags: tags}, 'haunted', {
fn: function(tags) {
should.exist(tags);
}
});
});
it('can call inverse function if tag is not found', function() {
helpers.has_tag.call({tags: tags}, 'undefined', {
inverse: function(tags) {
should.exist(tags);
}
});
});
});
});