From 27ba9289d6772ba31adfb51e2db70893376e7d7a Mon Sep 17 00:00:00 2001 From: Sebastian Gierlinger Date: Thu, 5 Sep 2013 14:55:28 +0200 Subject: [PATCH] Add setting for posts per page closes #593 - added default setting of 6 posts per page - added posts per page to settings page - added limit to frontend.js (setting does not change API behavior) --- core/client/tpl/settings/general.hbs | 7 ++++++- core/client/views/settings.js | 3 ++- core/server/controllers/frontend.js | 16 +++++++++++++--- core/server/data/default-settings.json | 6 ++++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/core/client/tpl/settings/general.hbs b/core/client/tpl/settings/general.hbs index cc344f147f..9d95b01561 100644 --- a/core/client/tpl/settings/general.hbs +++ b/core/client/tpl/settings/general.hbs @@ -57,10 +57,15 @@ +
+ + +

Number of posts per page displayed

+
diff --git a/core/client/views/settings.js b/core/client/views/settings.js index 4263a4d657..bcc64a0f27 100644 --- a/core/client/views/settings.js +++ b/core/client/views/settings.js @@ -164,7 +164,8 @@ email: this.$('#email-address').val(), logo: this.$('#logo').attr("src"), icon: this.$('#icon').attr("src"), - activeTheme: this.$('#activeTheme').val() + activeTheme: this.$('#activeTheme').val(), + postsPerPage: this.$('#postsPerPage').val() }, { success: this.saveSuccess, error: this.saveError diff --git a/core/server/controllers/frontend.js b/core/server/controllers/frontend.js index e4b0deecaa..461dc8699d 100644 --- a/core/server/controllers/frontend.js +++ b/core/server/controllers/frontend.js @@ -14,14 +14,24 @@ var Ghost = require('../../ghost'), frontendControllers = { 'homepage': function (req, res) { // Parse the page number - var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1; + var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1, + postsPerPage = parseInt(ghost.settings().postsPerPage, 10), + options = {}; // No negative pages - if (pageParam < 1) { + if (isNaN(pageParam) || pageParam < 1) { + //redirect to 404 page? return res.redirect("/page/1/"); } + options.page = pageParam; + + // No negative posts per page, must be number + if (!isNaN(postsPerPage) && postsPerPage > 0) { + options.limit = postsPerPage; + } + + api.posts.browse(options).then(function (page) { - api.posts.browse({page: pageParam}).then(function (page) { var maxPage = page.pages; // A bit of a hack for situations with no content. diff --git a/core/server/data/default-settings.json b/core/server/data/default-settings.json index 0800b357e3..d0dd67abc9 100644 --- a/core/server/data/default-settings.json +++ b/core/server/data/default-settings.json @@ -43,5 +43,11 @@ "key": "icon", "value": "", "type": "blog" + }, + { + "key": "postsPerPage", + "value": "6", + "type": "blog" } + ]