Added bodyclass and postclass helper

closes #472

- filterable as array, output as string
- with unit tests
This commit is contained in:
Hannah Wolfe 2013-08-22 20:57:53 +01:00
parent 2d3e2f36b1
commit 98e4923077
2 changed files with 61 additions and 0 deletions

View File

@ -89,6 +89,32 @@ coreHelpers = function (ghost) {
);
});
ghost.registerThemeHelper('bodyclass', function (options) {
var classes = [];
if (!this.path || this.path === '/' || this.path === '') {
classes.push('home');
} else {
classes.push('post');
}
return ghost.doFilter('bodyclass', classes, function (classes) {
var classString = _.reduce(classes, function (memo, item) { return memo + ' ' + item; }, '');
return new hbs.handlebars.SafeString(classString.trim());
});
});
ghost.registerThemeHelper('postclass', function (options) {
var classes = ['post'];
// TODO: add tag names once we have them
return ghost.doFilter('postclass', classes, function (classes) {
var classString = _.reduce(classes, function (memo, item) { return memo + ' ' + item; }, '');
return new hbs.handlebars.SafeString(classString.trim());
});
});
/**
* [ description]
*

View File

@ -116,8 +116,43 @@ describe('Core Helpers', function () {
should.exist(rendered);
rendered.string.should.equal(expected);
});
});
describe('Bodyclass Helper', function () {
it('has loaded bodyclass helper', function () {
should.exist(handlebars.helpers.bodyclass);
});
it('can render class string', function () {
var rendered = handlebars.helpers.bodyclass.call({});
should.exist(rendered);
rendered.string.should.equal('home');
});
it('can render class string for context', function () {
var rendered1 = handlebars.helpers.bodyclass.call({path: '/'}),
rendered2 = handlebars.helpers.bodyclass.call({path: '/a-post-title'});
should.exist(rendered1);
should.exist(rendered2);
rendered1.string.should.equal('home');
rendered2.string.should.equal('post');
});
});
describe('Postclass Helper', function () {
it('has loaded postclass helper', function () {
should.exist(handlebars.helpers.postclass);
});
it('can render class string', function () {
var rendered = handlebars.helpers.postclass.call({});
should.exist(rendered);
rendered.string.should.equal('post');
});
});
describe('Navigation Helper', function () {