Ghost/core/frontend/helpers/navigation.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

46 lines
1.4 KiB
JavaScript

var fs = require('fs'),
path = require('path'),
_ = require('underscore'),
handlebars = require('express-hbs').handlebars,
nodefn = require('when/node/function'),
NavHelper;
NavHelper = function (navTemplate) {
// Bind the context for our methods.
_.bindAll(this, 'compileTemplate', 'renderNavItems');
if (_.isFunction(navTemplate)) {
this.navTemplateFunc = navTemplate;
} else {
this.navTemplatePath = navTemplate;
}
};
NavHelper.prototype.compileTemplate = function (templatePath) {
var self = this;
// Allow people to overwrite the navTemplatePath
templatePath = templatePath || this.navTemplatePath;
return nodefn.call(fs.readFile, templatePath).then(function (navTemplateContents) {
// TODO: Can handlebars compile async?
self.navTemplateFunc = handlebars.compile(navTemplateContents.toString());
});
};
NavHelper.prototype.renderNavItems = function (navItems) {
return new handlebars.SafeString(this.navTemplateFunc({links: navItems}));
};
// A static helper method for registering with ghost
NavHelper.registerWithGhost = function (ghost) {
var templatePath = path.join(ghost.paths().frontendViews, 'nav.hbs'),
ghostNavHelper = new NavHelper(templatePath);
return ghostNavHelper.compileTemplate().then(function () {
ghost.registerThemeHelper("nav", ghostNavHelper.renderNavItems);
});
};
module.exports = NavHelper;