Skip to content

Commit

Permalink
added suffix to tag helper
Browse files Browse the repository at this point in the history
closes TryGhost#607

- added suffix as optional parameter to tag helper
  • Loading branch information
cobbspur committed Oct 28, 2013
1 parent 46dc171 commit d605100
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
27 changes: 8 additions & 19 deletions core/server/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,14 @@ coreHelpers = function (ghost) {
// Note that the standard {{#each tags}} implementation is unaffected by this helper
// 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');

if (tagNames.length && prefix) {
output = prefix + tagNames.join(separator);
} else {
output = tagNames.join(separator);
var separator = _.isString(options.hash.separator) ? options.hash.separator : ', ',
prefix = _.isString(options.hash.prefix) ? options.hash.prefix : '',
suffix = _.isString(options.hash.suffix) ? options.hash.suffix : '',
output = '',
tagNames = _.pluck(this.tags, 'name');

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

return output;
Expand Down
28 changes: 26 additions & 2 deletions core/test/unit/server_helpers_index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,34 @@ describe('Core Helpers', function () {
String(rendered).should.equal('on haunted, ghost');
});

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

should.exist(rendered);

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

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

should.exist(rendered);

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

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

should.exist(rendered);
Expand Down

0 comments on commit d605100

Please sign in to comment.