Skip to content

Commit

Permalink
🐛 Fixed {{body_class}} helper when using data: page.{slug} in routes
Browse files Browse the repository at this point in the history
refs TryGhost#10082

- throwed a 500 because this.page was not handled
- v2 differentiates between page and post
  • Loading branch information
kirrg001 committed Mar 12, 2019
1 parent fdb77f9 commit 1b2e7c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
21 changes: 11 additions & 10 deletions core/server/helpers/body_class.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ var proxy = require('./proxy'),

// We use the name body_class to match the helper for consistency:
module.exports = function body_class(options) { // eslint-disable-line camelcase
var classes = [],
context = options.data.root.context,
post = this.post,
tags = this.post && this.post.tags ? this.post.tags : this.tags || [],
page = this.post && this.post.page ? this.post.page : this.page || false;
let classes = [];
const context = options.data.root.context;
const obj = this.post || this.page;
const tags = obj && obj.tags ? obj.tags : [];
const isPage = !!(obj && obj.page);

if (_.includes(context, 'home')) {
classes.push('home-template');
} else if (_.includes(context, 'post') && post) {
} else if (_.includes(context, 'post') && obj) {
classes.push('post-template');
} else if (_.includes(context, 'page') && page) {
} else if (_.includes(context, 'page') && obj && isPage) {
classes.push('page-template');
classes.push('page-' + this.post.slug);
classes.push('page-' + obj.slug);
} else if (_.includes(context, 'tag') && this.tag) {
classes.push('tag-template');
classes.push('tag-' + this.tag.slug);
Expand All @@ -33,7 +33,7 @@ module.exports = function body_class(options) { // eslint-disable-line camelcase

if (tags) {
classes = classes.concat(tags.map(function (tag) {
return 'tag-' + tag.slug;
return 'tag-' + tag.slug;
}));
}

Expand All @@ -42,8 +42,9 @@ module.exports = function body_class(options) { // eslint-disable-line camelcase
}

classes = _.reduce(classes, function (memo, item) {
return memo + ' ' + item;
return memo + ' ' + item;
}, '');

return new SafeString(classes.trim());
};

11 changes: 10 additions & 1 deletion core/test/unit/helpers/body_class_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('{{body_class}} helper', function () {
rendered.string.should.equal('post-template tag-foo tag-bar');
});

it('a static page', function () {
it('v0.1: a static page', function () {
var rendered = callBodyClassWithContext(
['page'],
{relativeUrl: '/about', post: {page: true, slug: 'about'}}
Expand All @@ -142,6 +142,15 @@ describe('{{body_class}} helper', function () {
rendered.string.should.equal('page-template page-about');
});

it('v2: a static page', function () {
var rendered = callBodyClassWithContext(
['page'],
{relativeUrl: '/about', page: {page: true, slug: 'about'}}
);

rendered.string.should.equal('page-template page-about');
});

it('a static page with custom template (is now the same as one without)', function () {
var rendered = callBodyClassWithContext(
['page'],
Expand Down

0 comments on commit 1b2e7c9

Please sign in to comment.