Merge pull request #541 from cobbspur/helpers3

added url handlebars helper
This commit is contained in:
Hannah Wolfe 2013-08-27 15:10:02 -07:00
commit 02a8482447
3 changed files with 31 additions and 0 deletions

View File

@ -4,8 +4,10 @@ var _ = require('underscore'),
when = require('when'),
hbs = require('express-hbs'),
errors = require('../errorHandling'),
models = require('../models'),
coreHelpers;
coreHelpers = function (ghost) {
var navHelper,
paginationHelper;
@ -29,6 +31,12 @@ coreHelpers = function (ghost) {
return date;
});
ghost.registerThemeHelper('url', function (context, options) {
if (models.isPost(this)) {
return "/" + this.slug;
}
return '';
});
// ### Author Helper
//

View File

@ -13,5 +13,9 @@ module.exports = {
return migrations.reset().then(function () {
return migrations.init();
});
},
isPost: function (jsonData) {
return jsonData.hasOwnProperty("content") && jsonData.hasOwnProperty("content_raw")
&& jsonData.hasOwnProperty("title") && jsonData.hasOwnProperty("slug");
}
};

View File

@ -201,6 +201,25 @@ describe('Core Helpers', function () {
});
});
describe('url Helper', function () {
it('has loaded url helper', function () {
should.exist(handlebars.helpers.url);
});
it('if context is post, returns a the slug with a prefix slash', function () {
var rendered = handlebars.helpers.url.call({content: 'content', content_raw: "ff", title: "title", slug: "slug"});
should.exist(rendered);
rendered.should.equal('/slug');
});
it('it should return empty string if not a post', function () {
handlebars.helpers.url.call({content_raw: "ff", title: "title", slug: "slug"}).should.equal('');
handlebars.helpers.url.call({content: 'content', title: "title", slug: "slug"}).should.equal('');
handlebars.helpers.url.call({content: 'content', content_raw: "ff", slug: "slug"}).should.equal('');
handlebars.helpers.url.call({content: 'content', content_raw: "ff", title: "title"}).should.equal('');
});
});
describe('Navigation Helper', function () {
it('has loaded nav helper', function () {