-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1606 from Gotvitch/issue1203
Bug fixes for partial views
- Loading branch information
Showing
7 changed files
with
166 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,27 @@ | ||
var templates = {}, | ||
nodefn = require('when/node/function'), | ||
fs = require('fs'), | ||
hbs = require('express-hbs'), | ||
errors = require('../errorHandling'), | ||
path = require('path'), | ||
when = require('when'), | ||
config = require('../config'), | ||
api = require('../api'); | ||
errors = require('../errorHandling'); | ||
|
||
// ## Template utils | ||
|
||
// Compile a template for a handlebars helper | ||
templates.compileTemplate = function (templatePath) { | ||
return nodefn.call(fs.readFile, templatePath).then(function (templateContents) { | ||
return hbs.handlebars.compile(templateContents.toString()); | ||
}, errors.logAndThrowError); | ||
}; | ||
// Execute a template helper | ||
// All template helpers are register as partial view. | ||
templates.execute = function (name, context) { | ||
|
||
// Load a template for a handlebars helper | ||
templates.loadTemplate = function (name) { | ||
var templateFileName = name + '.hbs', | ||
deferred = when.defer(); | ||
// Check for theme specific version first | ||
return api.settings.read('activeTheme').then(function (activeTheme) { | ||
var templatePath = path.join(config.paths().themePath, activeTheme.value, 'partials', templateFileName); | ||
var partial = hbs.handlebars.partials[name]; | ||
|
||
// 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(config.paths().helperTemplates, templateFileName); | ||
} | ||
if (partial === undefined) { | ||
errors.logAndThrowError('Template ' + name + ' not found.'); | ||
return; | ||
} | ||
|
||
templates.compileTemplate(templatePath).then(deferred.resolve, deferred.reject); | ||
}); | ||
// If the partial view is not compiled, it compiles and saves in handlebars | ||
if (typeof partial === 'string') { | ||
partial = hbs.handlebars.compile(partial); | ||
hbs.handlebars.partials[name] = partial; | ||
} | ||
|
||
return deferred.promise; | ||
}); | ||
return new hbs.handlebars.SafeString(partial(context)); | ||
}; | ||
|
||
module.exports = templates; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.