0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Merge pull request #1539 from niedbalski/master

[Feature Request] Theme API: add has_tag helper to coreHelpers.
This commit is contained in:
Hannah Wolfe 2013-11-24 06:19:18 -08:00
commit 80eb56edd2
2 changed files with 45 additions and 0 deletions

View file

@ -466,6 +466,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;
@ -538,6 +556,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);
}
});
});
});
});