mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Merge pull request #3415 from PaulAdamDavis/plural-helper
Plural handlebars helper
This commit is contained in:
commit
c094facb28
2 changed files with 79 additions and 0 deletions
|
@ -702,6 +702,28 @@ coreHelpers.pagination = function (options) {
|
||||||
return template.execute('pagination', context);
|
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) {
|
coreHelpers.helperMissing = function (arg) {
|
||||||
if (arguments.length === 2) {
|
if (arguments.length === 2) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -747,6 +769,8 @@ function registerAdminHelper(name, fn) {
|
||||||
coreHelpers.adminHbs.registerHelper(name, fn);
|
coreHelpers.adminHbs.registerHelper(name, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
registerHelpers = function (adminHbs, assetHash) {
|
registerHelpers = function (adminHbs, assetHash) {
|
||||||
|
|
||||||
// Expose hbs instance for admin
|
// Expose hbs instance for admin
|
||||||
|
@ -781,6 +805,8 @@ registerHelpers = function (adminHbs, assetHash) {
|
||||||
|
|
||||||
registerThemeHelper('tags', coreHelpers.tags);
|
registerThemeHelper('tags', coreHelpers.tags);
|
||||||
|
|
||||||
|
registerThemeHelper('plural', coreHelpers.plural);
|
||||||
|
|
||||||
registerAsyncThemeHelper('body_class', coreHelpers.body_class);
|
registerAsyncThemeHelper('body_class', coreHelpers.body_class);
|
||||||
|
|
||||||
registerAsyncThemeHelper('e', coreHelpers.e);
|
registerAsyncThemeHelper('e', coreHelpers.e);
|
||||||
|
|
|
@ -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 () {
|
describe('Excerpt Helper', function () {
|
||||||
|
|
||||||
it('has loaded excerpt helper', function () {
|
it('has loaded excerpt helper', function () {
|
||||||
|
|
Loading…
Add table
Reference in a new issue