From d40c08218e9167c9163128b35b80cb1c2b666202 Mon Sep 17 00:00:00 2001 From: Jacob Gable Date: Fri, 2 Aug 2013 12:51:33 -0500 Subject: [PATCH] Allow theme partials to override helper templates Added an existence check to the loadTemplates function --- core/ghost.js | 20 ++++++-- .../unit/fixtures/theme/partials/test.hbs | 1 + core/test/unit/ghost_spec.js | 46 +++++++++++++++++-- 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 core/test/unit/fixtures/theme/partials/test.hbs diff --git a/core/ghost.js b/core/ghost.js index 99201d3bf2..4a5d96e71a 100644 --- a/core/ghost.js +++ b/core/ghost.js @@ -178,10 +178,24 @@ Ghost.prototype.compileTemplate = function (templatePath) { }; Ghost.prototype.loadTemplate = function (name) { - // TODO: allow themes to override these templates - var templatePath = path.join(this.paths().helperTemplates, name + '.hbs'); + var self = this, + templateFileName = name + '.hbs', + // Check for theme specific version first + templatePath = path.join(this.paths().activeTheme, "partials", templateFileName), + deferred = when.defer(); - return this.compileTemplate(templatePath); + // Can't use nodefn here because exists just returns one parameter, true or false + + fs.exists(templatePath, function (exists) { + if (!exists) { + // Fall back to helpers templates location + templatePath = path.join(self.paths().helperTemplates, templateFileName); + } + + self.compileTemplate(templatePath).then(deferred.resolve, deferred.reject); + }); + + return deferred.promise; }; /** diff --git a/core/test/unit/fixtures/theme/partials/test.hbs b/core/test/unit/fixtures/theme/partials/test.hbs new file mode 100644 index 0000000000..31e6fea5e8 --- /dev/null +++ b/core/test/unit/fixtures/theme/partials/test.hbs @@ -0,0 +1 @@ +

HelloWorld Themed

\ No newline at end of file diff --git a/core/test/unit/ghost_spec.js b/core/test/unit/ghost_spec.js index 5f7077a502..ecde0ec5fa 100644 --- a/core/test/unit/ghost_spec.js +++ b/core/test/unit/ghost_spec.js @@ -8,6 +8,7 @@ var should = require('should'), describe("Ghost API", function () { var testTemplatePath = 'core/test/unit/fixtures/', + themeTemplatePath= 'core/test/unit/fixtures/theme', ghost; beforeEach(function () { @@ -111,16 +112,24 @@ describe("Ghost API", function () { }); it("loads templates for helpers", function (done) { - var compileSpy = sinon.spy(ghost, 'compileTemplate'); + var compileSpy = sinon.spy(ghost, 'compileTemplate'), + pathsStub; should.exist(ghost.loadTemplate, 'load template function exists'); // In order for the test to work, need to replace the path to the template - ghost.paths = sinon.stub().returns({ - helperTemplates: path.join(process.cwd(), testTemplatePath) + pathsStub = sinon.stub(ghost, "paths", function () { + return { + // Forcing the theme path to be the same + activeTheme: path.join(process.cwd(), testTemplatePath), + helperTemplates: path.join(process.cwd(), testTemplatePath) + }; }); ghost.loadTemplate('test').then(function (templateFn) { + compileSpy.restore(); + pathsStub.restore(); + // test that compileTemplate was called with the expected path compileSpy.calledOnce.should.equal(true); compileSpy.calledWith(path.join(process.cwd(), testTemplatePath, 'test.hbs')).should.equal(true); @@ -130,7 +139,38 @@ describe("Ghost API", function () { templateFn().should.equal('

HelloWorld

'); + + + done(); + }).then(null, done); + }); + + it("loads templates from themes first", function (done) { + var compileSpy = sinon.spy(ghost, 'compileTemplate'), + pathsStub; + + should.exist(ghost.loadTemplate, 'load template function exists'); + + // In order for the test to work, need to replace the path to the template + pathsStub = sinon.stub(ghost, "paths", function () { + return { + activeTheme: path.join(process.cwd(), themeTemplatePath), + helperTemplates: path.join(process.cwd(), testTemplatePath) + }; + }); + + ghost.loadTemplate('test').then(function (templateFn) { + // test that compileTemplate was called with the expected path + compileSpy.calledOnce.should.equal(true); + compileSpy.calledWith(path.join(process.cwd(), themeTemplatePath, 'partials', 'test.hbs')).should.equal(true); + + should.exist(templateFn); + _.isFunction(templateFn).should.equal(true); + + templateFn().should.equal('

HelloWorld Themed

'); + compileSpy.restore(); + pathsStub.restore(); done(); }).then(null, done);