Improving tag handling in post_class and body_class

closes #967, closes #987

- use slug instead of name (it's unique)
- get tags even if we aren't inside the post context
- add tag handling to body_class too
This commit is contained in:
Hannah Wolfe 2013-10-09 19:51:55 +01:00
parent d169bba3f8
commit f1317b84af

View File

@ -179,7 +179,9 @@ coreHelpers = function (ghost) {
ghost.registerThemeHelper('body_class', function (options) {
var classes = [];
var classes = [],
tags = this.post && this.post.tags ? this.post.tags : this.tags || [];
if (_.isString(this.path) && this.path.match(/\/page/)) {
classes.push('archive-template');
} else if (!this.path || this.path === '/' || this.path === '') {
@ -188,6 +190,10 @@ coreHelpers = function (ghost) {
classes.push('post-template');
}
if (tags) {
classes = classes.concat(tags.map(function (tag) { return 'tag-' + tag.slug; }));
}
return ghost.doFilter('body_class', classes, function (classes) {
var classString = _.reduce(classes, function (memo, item) { return memo + ' ' + item; }, '');
return new hbs.handlebars.SafeString(classString.trim());
@ -195,10 +201,11 @@ coreHelpers = function (ghost) {
});
ghost.registerThemeHelper('post_class', function (options) {
var classes = ['post'];
var classes = ['post'],
tags = this.post && this.post.tags ? this.post.tags : this.tags || [];
if (this.tags) {
classes = classes.concat(this.tags.map(function (tag) { return 'tag-' + tag.name; }));
if (tags) {
classes = classes.concat(tags.map(function (tag) { return 'tag-' + tag.slug; }));
}
return ghost.doFilter('post_class', classes, function (classes) {