Skip to content

Commit

Permalink
adds prefix option to tag helper
Browse files Browse the repository at this point in the history
        closes TryGhost#607

 - added prefix attributetor tags helper
 - will add prefix only if tags are present
 - adds unit tests for prefix
  • Loading branch information
cobbspur committed Oct 22, 2013
1 parent b319e5b commit b85e5b6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
15 changes: 14 additions & 1 deletion core/server/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,27 @@ coreHelpers = function (ghost) {
// and can be used for more complex templates.
ghost.registerThemeHelper('tags', function (options) {
var separator = ', ',
prefix,
output,
tagNames;

if (_.isString(options.hash.separator)) {
separator = options.hash.separator;
}

if (_.isString(options.hash.prefix)) {
prefix = options.hash.prefix;
}

tagNames = _.pluck(this.tags, 'name');
return tagNames.join(separator);

if (tagNames.length && prefix) {
output = prefix + tagNames.join(separator);
} else {
output = tagNames.join(separator);
}

return output;
});

// ### Content Helper
Expand Down
29 changes: 26 additions & 3 deletions core/test/unit/server_helpers_index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ describe('Core Helpers', function () {
});

it('can return string with tags', function () {
var tags = [{name:'foo'}, {name:'bar'}],
var tags = [{name: 'foo'}, {name: 'bar'}],
rendered = handlebars.helpers.tags.call(
{tags: tags},
{tags: tags},
{"hash": {}}
);
should.exist(rendered);
Expand All @@ -410,7 +410,7 @@ describe('Core Helpers', function () {
});

it('can use a different separator', function () {
var tags = [{name:'haunted'},{name:'ghost'}],
var tags = [{name: 'haunted'}, {name: 'ghost'}],
rendered = handlebars.helpers.tags.call(
{tags: tags},
{"hash": {separator: '|'}}
Expand All @@ -420,6 +420,29 @@ describe('Core Helpers', function () {

String(rendered).should.equal('haunted|ghost');
});

it('can add a single prefix to multiple tags', function () {
var tags = [{name: 'haunted'}, {name: 'ghost'}],
rendered = handlebars.helpers.tags.call(
{tags: tags},
{"hash": {prefix: 'on '}}
);

should.exist(rendered);

String(rendered).should.equal('on haunted, ghost');
});

it('does not add prefix if no tags exist', function () {
var rendered = handlebars.helpers.tags.call(
{},
{"hash": {prefix: 'on '}}
);

should.exist(rendered);

String(rendered).should.equal('');
});
});

describe("meta_title helper", function () {
Expand Down

0 comments on commit b85e5b6

Please sign in to comment.