diff --git a/core/server/helpers/index.js b/core/server/helpers/index.js index de419e7eed..333f5cacb3 100644 --- a/core/server/helpers/index.js +++ b/core/server/helpers/index.js @@ -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); diff --git a/core/test/unit/server_helpers_index_spec.js b/core/test/unit/server_helpers_index_spec.js index ffe71aa3fa..eb86cd7e27 100644 --- a/core/test/unit/server_helpers_index_spec.js +++ b/core/test/unit/server_helpers_index_spec.js @@ -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); + } + }); + }); + + }); });