From 4e85ac1c94029daac951b3533f9f0fe80f224eac Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 29 Jul 2014 17:26:04 +0100 Subject: [PATCH] Adding new {{title}} helper closes #3389 --- core/server/helpers/index.js | 6 ++++ core/test/unit/server_helpers_index_spec.js | 34 ++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/core/server/helpers/index.js b/core/server/helpers/index.js index 262f614403..c28ecd756b 100644 --- a/core/server/helpers/index.js +++ b/core/server/helpers/index.js @@ -298,6 +298,10 @@ coreHelpers.content = function (options) { return new hbs.handlebars.SafeString(this.html); }; +coreHelpers.title = function () { + return new hbs.handlebars.SafeString(hbs.handlebars.Utils.escapeExpression(this.title || '')); +}; + // ### Excerpt Helper // // *Usage example:* @@ -787,6 +791,8 @@ registerHelpers = function (adminHbs, assetHash) { registerThemeHelper('content', coreHelpers.content); + registerThemeHelper('title', coreHelpers.title); + registerThemeHelper('date', coreHelpers.date); registerThemeHelper('encode', coreHelpers.encode); diff --git a/core/test/unit/server_helpers_index_spec.js b/core/test/unit/server_helpers_index_spec.js index 82760535d0..3c3ad49d4d 100644 --- a/core/test/unit/server_helpers_index_spec.js +++ b/core/test/unit/server_helpers_index_spec.js @@ -169,6 +169,38 @@ describe('Core Helpers', function () { }); }); + describe('Title Helper', function () { + it('has loaded title helper', function () { + should.exist(handlebars.helpers.title); + }); + + it('can render title', function () { + var title = 'Hello World', + rendered = helpers.title.call({title: title}); + + should.exist(rendered); + rendered.string.should.equal(title); + }); + + it('escapes correctly', function () { + var rendered = helpers.title.call({'title': '

I am a title

'}); + + rendered.string.should.equal('<h1>I am a title</h1>'); + }); + + it('returns a blank string where title is missing', function () { + var rendered = helpers.title.call({'title': null}); + + rendered.string.should.equal(''); + }); + + it('returns a blank string where data missing', function () { + var rendered = helpers.title.call({}); + + rendered.string.should.equal(''); + }); + }); + describe('Author Helper', function () { it('has loaded author helper', function () { @@ -215,7 +247,7 @@ describe('Core Helpers', function () { }); }); - + describe('Plural Helper', function () {