Skip to content

Commit 2cb02b5

Browse files
committed
Custom Page Templates
fixes #1969 - creates new ./server/helpers/tempalte.js method which returns the correct view to use when rendering - updates fronted controller to check if a custom page template exists and if so then uses that to render the static page - adds additional class name to body_class helper when a custom page template is being rendered - adds tests to address all new features
1 parent 4b64336 commit 2cb02b5

File tree

6 files changed

+198
-62
lines changed

6 files changed

+198
-62
lines changed

core/server/controllers/frontend.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var moment = require('moment'),
1515
config = require('../config'),
1616
errors = require('../errorHandling'),
1717
filters = require('../../server/filters'),
18+
template = require('../helpers/template'),
1819

1920
frontendControllers,
2021
// Cache static post permalink regex
@@ -186,7 +187,8 @@ frontendControllers = {
186187
filters.doFilter('prePostsRender', post).then(function (post) {
187188
api.settings.read('activeTheme').then(function (activeTheme) {
188189
var paths = config().paths.availableThemes[activeTheme.value],
189-
view = post.page && paths.hasOwnProperty('page.hbs') ? 'page' : 'post';
190+
view = template.getThemeViewForPost(paths, post);
191+
190192
res.render(view, {post: post});
191193
});
192194
});

core/server/helpers/index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ coreHelpers.ghost_script_tags = function () {
347347
coreHelpers.body_class = function (options) {
348348
/*jslint unparam:true*/
349349
var classes = [],
350+
post = this.post,
350351
tags = this.post && this.post.tags ? this.post.tags : this.tags || [],
351352
page = this.post && this.post.page ? this.post.page : this.page || false;
352353

@@ -366,9 +367,26 @@ coreHelpers.body_class = function (options) {
366367
classes.push('page');
367368
}
368369

369-
return filters.doFilter('body_class', classes).then(function (classes) {
370-
var classString = _.reduce(classes, function (memo, item) { return memo + ' ' + item; }, '');
371-
return new hbs.handlebars.SafeString(classString.trim());
370+
return api.settings.read('activeTheme').then(function (activeTheme) {
371+
var paths = config().paths.availableThemes[activeTheme.value],
372+
view;
373+
374+
if (post) {
375+
view = template.getThemeViewForPost(paths, post).split('-');
376+
377+
// If this is a page and we have a custom page template
378+
// then we need to modify the class name we inject
379+
// e.g. 'page-contact' is outputted as 'page-template-contact'
380+
if (view[0] === 'page' && view.length > 1) {
381+
view.splice(1, 0, 'template');
382+
classes.push(view.join('-'));
383+
}
384+
}
385+
386+
return filters.doFilter('body_class', classes).then(function (classes) {
387+
var classString = _.reduce(classes, function (memo, item) { return memo + ' ' + item; }, '');
388+
return new hbs.handlebars.SafeString(classString.trim());
389+
});
372390
});
373391
};
374392

core/server/helpers/template.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,26 @@ templates.execute = function (name, context) {
2323
return new hbs.handlebars.SafeString(partial(context));
2424
};
2525

26+
// Given a theme object and a post object this will return
27+
// which theme template page should be used.
28+
// If given a post object that is a regular post
29+
// it will return 'post'.
30+
// If given a static post object it will return 'page'.
31+
// If given a static post object and a custom page template
32+
// exits it will return that page.
33+
templates.getThemeViewForPost = function (themePaths, post) {
34+
var customPageView = 'page-' + post.slug,
35+
view = 'post';
36+
37+
if (post.page) {
38+
if (themePaths.hasOwnProperty(customPageView + '.hbs')) {
39+
view = customPageView;
40+
} else if (themePaths.hasOwnProperty('page.hbs')) {
41+
view = 'page';
42+
}
43+
}
44+
45+
return view;
46+
};
47+
2648
module.exports = templates;

0 commit comments

Comments
 (0)