0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Add plural handlebars helper

Closes #3414

- Adds a helper which shows a select string deopending on the number of items provided

```
{{plural pagination.total empty='No posts' singular='% post' plural='% posts'}}
```
If `pagination.total` == 0, output 'No Posts'
If `pagination.total` == 1, outputs `1 Post`
If `pagination.total` ==  2 or more, outputs `2 Posts`

Credit to @sebgie for making this work.
This commit is contained in:
Paul Adam Davis 2014-07-25 16:31:18 +01:00
parent 49660ff703
commit 503f359aab
2 changed files with 79 additions and 0 deletions

View file

@ -702,6 +702,28 @@ coreHelpers.pagination = function (options) {
return template.execute('pagination', context);
};
// ## Pluralize strings depending on item count
// {{plural 0 empty='No posts' singular='% post' plural='% posts'}}
// The 1st argument is the numeric variable which the helper operates on
// The 2nd argument is the string that will be output if the variable's value is 0
// The 3rd argument is the string that will be output if the variable's value is 1
// The 4th argument is the string that will be output if the variable's value is 2+
// coreHelpers.plural = function (number, empty, singular, plural) {
coreHelpers.plural = function (context, options) {
if (_.isUndefined(options.hash) || _.isUndefined(options.hash.empty) ||
_.isUndefined(options.hash.singular) || _.isUndefined(options.hash.plural)) {
return errors.logAndThrowError('All values must be defined for empty, singular and plural');
}
if (context === 0) {
return new hbs.handlebars.SafeString(options.hash.empty);
} else if (context === 1) {
return new hbs.handlebars.SafeString(options.hash.singular.replace("%", context));
} else if (context >= 2) {
return new hbs.handlebars.SafeString(options.hash.plural.replace("%", context));
}
};
coreHelpers.helperMissing = function (arg) {
if (arguments.length === 2) {
return undefined;
@ -747,6 +769,8 @@ function registerAdminHelper(name, fn) {
coreHelpers.adminHbs.registerHelper(name, fn);
}
registerHelpers = function (adminHbs, assetHash) {
// Expose hbs instance for admin
@ -781,6 +805,8 @@ registerHelpers = function (adminHbs, assetHash) {
registerThemeHelper('tags', coreHelpers.tags);
registerThemeHelper('plural', coreHelpers.plural);
registerAsyncThemeHelper('body_class', coreHelpers.body_class);
registerAsyncThemeHelper('e', coreHelpers.e);

View file

@ -215,6 +215,59 @@ describe('Core Helpers', function () {
});
});
describe('Plural Helper', function () {
it('has loaded plural helper', function () {
should.exist(handlebars.helpers.plural);
});
it('will show no-value string', function () {
var expected = 'No Posts',
rendered = helpers.plural.call({}, 0, {
'hash': {
'empty': 'No Posts',
'singular': '% Post',
'plural': '% Posts'
}
});
should.exist(rendered);
rendered.string.should.equal(expected);
});
it('will show singular string', function () {
var expected = '1 Post',
rendered = helpers.plural.call({}, 1, {
'hash': {
'empty': 'No Posts',
'singular': '% Post',
'plural': '% Posts'
}
});
should.exist(rendered);
rendered.string.should.equal(expected);
});
it('will show plural string', function () {
var expected = '2 Posts',
rendered = helpers.plural.call({}, 2, {
'hash': {
'empty': 'No Posts',
'singular': '% Post',
'plural': '% Posts'
}
});
should.exist(rendered);
rendered.string.should.equal(expected);
});
});
describe('Excerpt Helper', function () {
it('has loaded excerpt helper', function () {