mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-29 13:52:10 +03:00
Merge pull request #331 from jgable/overrideTemplates
Allow theme partials to override helper templates
This commit is contained in:
commit
bb9a5226a8
@ -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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
1
core/test/unit/fixtures/theme/partials/test.hbs
Normal file
1
core/test/unit/fixtures/theme/partials/test.hbs
Normal file
@ -0,0 +1 @@
|
||||
<h1>HelloWorld Themed</h1>
|
@ -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('<h1>HelloWorld</h1>');
|
||||
|
||||
|
||||
|
||||
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('<h1>HelloWorld Themed</h1>');
|
||||
|
||||
compileSpy.restore();
|
||||
pathsStub.restore();
|
||||
|
||||
done();
|
||||
}).then(null, done);
|
||||
|
Loading…
Reference in New Issue
Block a user