diff --git a/core/server/helpers/index.js b/core/server/helpers/index.js index 94dd893db4..262f614403 100644 --- a/core/server/helpers/index.js +++ b/core/server/helpers/index.js @@ -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); diff --git a/core/test/unit/server_helpers_index_spec.js b/core/test/unit/server_helpers_index_spec.js index 34b8951965..82760535d0 100644 --- a/core/test/unit/server_helpers_index_spec.js +++ b/core/test/unit/server_helpers_index_spec.js @@ -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 () {