From be62a550f95b2d1cd97dfce8d6eeb64ec97c3e50 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Mon, 2 Sep 2013 18:27:26 +0100 Subject: [PATCH] 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 --- core/ghost.js | 28 +++++++++++++++++++++------- index.js | 6 ++---- package.json | 2 +- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/core/ghost.js b/core/ghost.js index 81f7d093a4..1e58a993ac 100644 --- a/core/ghost.js +++ b/core/ghost.js @@ -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'})); diff --git a/index.js b/index.js index 5e5441729f..f0a39b9919 100644 --- a/index.js +++ b/index.js @@ -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(); }); diff --git a/package.json b/package.json index d3c28225d2..a765fc0d04 100644 --- a/package.json +++ b/package.json @@ -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",