mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Merge pull request #3438 from hswolff/home-template
Add ability to create a 'home.hbs' template file for templates
This commit is contained in:
commit
e4134ccad7
2 changed files with 159 additions and 31 deletions
|
@ -98,6 +98,24 @@ function setReqCtx(req, data) {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the paths object of the active theme via way of a promise.
|
||||
* @return {Promise} The promise resolves with the value of the paths.
|
||||
*/
|
||||
function getActiveThemePaths() {
|
||||
return api.settings.read({
|
||||
key: 'activeTheme',
|
||||
context: {
|
||||
internal: true
|
||||
}
|
||||
}).then(function (response) {
|
||||
var activeTheme = response.settings[0],
|
||||
paths = config.paths.availableThemes[activeTheme.value];
|
||||
|
||||
return paths;
|
||||
});
|
||||
}
|
||||
|
||||
frontendControllers = {
|
||||
'homepage': function (req, res, next) {
|
||||
// Parse the page number
|
||||
|
@ -122,7 +140,17 @@ frontendControllers = {
|
|||
|
||||
// Render the page of posts
|
||||
filters.doFilter('prePostsRender', page.posts).then(function (posts) {
|
||||
res.render('index', formatPageResponse(posts, page));
|
||||
getActiveThemePaths().then(function (paths) {
|
||||
var view = paths.hasOwnProperty('home.hbs') ? 'home' : 'index';
|
||||
|
||||
// If we're on a page then we always render the index
|
||||
// template.
|
||||
if (pageParam > 1) {
|
||||
view = 'index';
|
||||
}
|
||||
|
||||
res.render(view, formatPageResponse(posts, page));
|
||||
});
|
||||
});
|
||||
}).otherwise(handleError(next));
|
||||
},
|
||||
|
@ -163,10 +191,8 @@ frontendControllers = {
|
|||
|
||||
// Render the page of posts
|
||||
filters.doFilter('prePostsRender', page.posts).then(function (posts) {
|
||||
api.settings.read({key: 'activeTheme', context: {internal: true}}).then(function (response) {
|
||||
var activeTheme = response.settings[0],
|
||||
paths = config.paths.availableThemes[activeTheme.value],
|
||||
view = paths.hasOwnProperty('tag.hbs') ? 'tag' : 'index',
|
||||
getActiveThemePaths().then(function (paths) {
|
||||
var view = paths.hasOwnProperty('tag.hbs') ? 'tag' : 'index',
|
||||
|
||||
// Format data for template
|
||||
result = _.extend(formatPageResponse(posts, page), {
|
||||
|
@ -222,10 +248,8 @@ frontendControllers = {
|
|||
|
||||
// Render the page of posts
|
||||
filters.doFilter('prePostsRender', page.posts).then(function (posts) {
|
||||
api.settings.read({key: 'activeTheme', context: {internal: true}}).then(function (response) {
|
||||
var activeTheme = response.settings[0],
|
||||
paths = config.paths.availableThemes[activeTheme.value],
|
||||
view = paths.hasOwnProperty('author.hbs') ? 'author' : 'index',
|
||||
getActiveThemePaths().then(function (paths) {
|
||||
var view = paths.hasOwnProperty('author.hbs') ? 'author' : 'index',
|
||||
|
||||
// Format data for template
|
||||
result = _.extend(formatPageResponse(posts, page), {
|
||||
|
@ -303,10 +327,8 @@ frontendControllers = {
|
|||
setReqCtx(req, post);
|
||||
|
||||
filters.doFilter('prePostsRender', post).then(function (post) {
|
||||
api.settings.read({key: 'activeTheme', context: {internal: true}}).then(function (response) {
|
||||
var activeTheme = response.settings[0],
|
||||
paths = config.paths.availableThemes[activeTheme.value],
|
||||
view = template.getThemeViewForPost(paths, post);
|
||||
getActiveThemePaths().then(function (paths) {
|
||||
var view = template.getThemeViewForPost(paths, post);
|
||||
|
||||
res.render(view, formatResponse(post));
|
||||
});
|
||||
|
|
|
@ -33,6 +33,15 @@ describe('Frontend Controller', function () {
|
|||
sandbox.restore();
|
||||
});
|
||||
|
||||
// Helper function to prevent unit tests
|
||||
// from failing via timeout when they
|
||||
// should just immediately fail
|
||||
function failTest(done, msg) {
|
||||
return function () {
|
||||
done(new Error(msg));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
describe('homepage redirects', function () {
|
||||
var res;
|
||||
|
@ -140,7 +149,120 @@ describe('Frontend Controller', function () {
|
|||
res.render.called.should.be.false;
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('homepage', function () {
|
||||
|
||||
beforeEach(function () {
|
||||
sandbox.stub(api.posts, 'browse', function () {
|
||||
return when({
|
||||
posts: [],
|
||||
meta: {
|
||||
pagination: {
|
||||
page: 1,
|
||||
pages: 3
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
apiSettingsStub = sandbox.stub(api.settings, 'read');
|
||||
|
||||
apiSettingsStub.withArgs(sinon.match.has('key', 'activeTheme')).returns(when({
|
||||
settings: [{
|
||||
'key': 'activeTheme',
|
||||
'value': 'casper'
|
||||
}]
|
||||
}));
|
||||
|
||||
apiSettingsStub.withArgs('postsPerPage').returns(when({
|
||||
settings: [{
|
||||
'key': 'postsPerPage',
|
||||
'value': '10'
|
||||
}]
|
||||
}));
|
||||
|
||||
frontend.__set__('config', {
|
||||
'paths': {
|
||||
'subdir': '',
|
||||
'availableThemes': {
|
||||
'casper': {
|
||||
'assets': null,
|
||||
'default.hbs': '/content/themes/casper/default.hbs',
|
||||
'index.hbs': '/content/themes/casper/index.hbs',
|
||||
'home.hbs': '/content/themes/casper/home.hbs',
|
||||
'page.hbs': '/content/themes/casper/page.hbs',
|
||||
'tag.hbs': '/content/themes/casper/tag.hbs'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('Renders home.hbs template when it exists in the active theme', function (done) {
|
||||
var req = {
|
||||
path: '/',
|
||||
params: {},
|
||||
route: {}
|
||||
},
|
||||
res = {
|
||||
render: function (view) {
|
||||
assert.equal(view, 'home');
|
||||
done();
|
||||
}
|
||||
};
|
||||
|
||||
frontend.homepage(req, res, failTest(done));
|
||||
});
|
||||
|
||||
it('Renders index.hbs template on 2nd page when home.bs exists', function (done) {
|
||||
var req = {
|
||||
path: '/page/2/',
|
||||
params: {
|
||||
page: 2
|
||||
},
|
||||
route: {}
|
||||
},
|
||||
res = {
|
||||
render: function (view) {
|
||||
assert.equal(view, 'index');
|
||||
done();
|
||||
}
|
||||
};
|
||||
|
||||
frontend.homepage(req, res, failTest(done));
|
||||
});
|
||||
|
||||
it('Renders index.hbs template when home.hbs doesn\'t exist', function (done) {
|
||||
frontend.__set__('config', {
|
||||
'paths': {
|
||||
'subdir': '',
|
||||
'availableThemes': {
|
||||
'casper': {
|
||||
'assets': null,
|
||||
'default.hbs': '/content/themes/casper/default.hbs',
|
||||
'index.hbs': '/content/themes/casper/index.hbs',
|
||||
'page.hbs': '/content/themes/casper/page.hbs',
|
||||
'tag.hbs': '/content/themes/casper/tag.hbs'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var req = {
|
||||
path: '/',
|
||||
params: {},
|
||||
route: {}
|
||||
},
|
||||
res = {
|
||||
render: function (view) {
|
||||
assert.equal(view, 'index');
|
||||
done();
|
||||
}
|
||||
};
|
||||
|
||||
frontend.homepage(req, res, failTest(done));
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -180,15 +302,7 @@ describe('Frontend Controller', function () {
|
|||
'name': 'audio',
|
||||
'slug': 'audio',
|
||||
'id': 2
|
||||
}],
|
||||
// Helper function to prevent unit tests
|
||||
// from failing via timeout when they
|
||||
// should just immediately fail
|
||||
failTest = function(done, msg) {
|
||||
return function() {
|
||||
done(new Error(msg));
|
||||
};
|
||||
};
|
||||
}];
|
||||
|
||||
beforeEach(function () {
|
||||
sandbox.stub(api.posts, 'browse', function () {
|
||||
|
@ -424,15 +538,7 @@ describe('Frontend Controller', function () {
|
|||
'email': 'test@ghost.org'
|
||||
}
|
||||
}]
|
||||
}],
|
||||
// Helper function to prevent unit tests
|
||||
// from failing via timeout when they
|
||||
// should just immediately fail
|
||||
failTest = function(done, msg) {
|
||||
return function() {
|
||||
done(new Error(msg));
|
||||
};
|
||||
};
|
||||
}];
|
||||
|
||||
beforeEach(function () {
|
||||
sandbox.stub(api.posts, 'read', function (args) {
|
||||
|
|
Loading…
Add table
Reference in a new issue