0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-13 22:41:32 -05:00
ghost/core/frontend/helpers/paginate.js
cobbspur 376cf647e5 closes #197 & closes #196 pagination wiring and helper
- adds routes for homepage pagination
- adds helper function to compile template file for pagination
- adds next and prev to post for next and previous page
- adds handlebars template for pagination
2013-06-25 16:13:19 +01:00

47 lines
No EOL
1.4 KiB
JavaScript

var fs = require('fs'),
path = require('path'),
_ = require('underscore'),
handlebars = require('express-hbs').handlebars,
nodefn = require('when/node/function'),
PaginationHelper;
PaginationHelper = function (paginationTemplate) {
// Bind the context for our methods.
_.bindAll(this, 'compileTemplate', 'renderPagination');
if (_.isFunction(paginationTemplate)) {
this.paginationTemplateFunc = paginationTemplate;
} else {
this.paginationTemplatePath = paginationTemplate;
}
};
PaginationHelper.prototype.compileTemplate = function (templatePath) {
var self = this;
templatePath = templatePath || this.paginationTemplatePath;
return nodefn.call(fs.readFile, templatePath).then(function (paginationContents) {
// TODO: Can handlebars compile async?
self.paginationTemplateFunc = handlebars.compile(paginationContents.toString());
});
};
PaginationHelper.prototype.renderPagination = function (context, options) {
var output = this.paginationTemplateFunc(context);
return output;
};
PaginationHelper.registerWithGhost = function (ghost) {
var templatePath = path.join(ghost.paths().frontendViews, 'pagination.hbs'),
paginationHelper = new PaginationHelper(templatePath);
return paginationHelper.compileTemplate().then(function () {
ghost.registerThemeHelper("paginate", paginationHelper.renderPagination);
});
};
module.exports = PaginationHelper;