Global settings for themes

issue #389

 - Upgrade express-hbs to get template options access
 - Grab config & settings with a hacky new method in ghost & pass to template options
 - Settings are no longer passed to res.locals
This commit is contained in:
Hannah Wolfe 2013-09-02 18:27:26 +01:00
parent 40c3b7fe31
commit be62a550f9
3 changed files with 24 additions and 12 deletions

View File

@ -97,6 +97,18 @@ Ghost = function () {
// there's no management here to be sure this has loaded
settings: function () { return instance.settingsCache; },
dataProvider: models,
blogGlobals: function () {
/* this is a bit of a hack until we have a better way to combine settings and config
* this data is what becomes globally available to themes */
return {
url: instance.config().env[process.env.NODE_ENV].url,
title: instance.settings().title,
description: instance.settings().description,
logo: instance.settings().logo,
/* urg.. need to fix paths */
themedir: path.join('/content/themes', instance.paths().activeTheme, instance.settingsCache.activeTheme)
};
},
statuses: function () { return statuses; },
polyglot: function () { return polyglot; },
getPaths: function () {
@ -296,27 +308,29 @@ Ghost.prototype.initPlugins = function (pluginsToLoad) {
// Initialise Theme or admin
Ghost.prototype.initTheme = function (app) {
var self = this;
var self = this,
hbsOptions;
return function initTheme(req, res, next) {
app.set('view engine', 'hbs');
// return the correct mime type for woff files
express['static'].mime.define({'application/font-woff': ['woff']});
if (!res.isAdmin) {
// self.globals is a hack til we have a better way of getting combined settings & config
hbsOptions = {templateOptions: {data: {blog: self.blogGlobals()}}};
if (!self.themeDirectories.hasOwnProperty(self.settings().activeTheme)) {
// Throw an error if the theme is not available...
// TODO: move this to happen on app start
errors.logAndThrowError('The currently active theme ' + self.settings().activeTheme + ' is missing.');
} else if (self.themeDirectories[self.settings().activeTheme].hasOwnProperty('partials')) {
// Check that the theme has a partials directory before trying to use it
app.engine('hbs', hbs.express3(
{partialsDir: path.join(self.paths().activeTheme, 'partials')}
));
} else {
// No partial dir, so no need to configure it
app.engine('hbs', hbs.express3());
hbsOptions.partialsDir = path.join(self.paths().activeTheme, 'partials');
}
app.engine('hbs', hbs.express3(hbsOptions));
app.set('views', self.paths().activeTheme);
} else {
app.engine('hbs', hbs.express3({partialsDir: self.paths().adminViews + 'partials'}));

View File

@ -108,10 +108,8 @@ function ghostLocals(req, res, next) {
if (!res.isAdmin) {
// filter the navigation items
ghost.doFilter('ghostNavItems', {path: req.path, navItems: []}, function (navData) {
// pass the theme navigation items and settings
_.extend(res.locals, navData, {
settings: ghost.settings()
});
// pass the theme navigation items, settings get configured as globals
_.extend(res.locals, navData);
next();
});

View File

@ -12,7 +12,7 @@
"engineStrict": true,
"dependencies": {
"express": "3.3.4",
"express-hbs": "0.2.0",
"express-hbs": "0.2.1",
"node-polyglot": "0.2.1",
"moment": "2.1.0",
"underscore": "1.5.1",