0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00
ghost/core/frontend/helpers/paginate.js
ErisDS 53fe5e3ba3 HTML helpers work with double taches - issue #246 item 1.
- updated navigation and pagination helpers to use SafeString
- nav and pagination don't need triple taches any more
- nav tests updated, and renamed to match helper name
2013-07-11 01:30:29 +01:00

45 lines
No EOL
1.5 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;
// Allow people to overwrite the paginationTemplatePath
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) {
return new handlebars.SafeString(this.paginationTemplateFunc(context));
};
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;