0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/frontend/services/themes/handlebars/register.js
Hannah Wolfe 59b9f161dd Moved non-helper code out of helpers
- the helper dir also contained some code used with helpers - utils and helper-helpers?
- the goal here was for helpers to be the only thing in their folder so we can look at moving them out
- all other code has been moved to services/themes for now, which is not the right place either
- services/themes is a catch-all for theme storage, loading, validation, rendering and more, needs to be broken down
2020-04-10 12:27:43 +01:00

43 lines
1.6 KiB
JavaScript

const Promise = require('bluebird');
const hbs = require('../engine');
const config = require('../../../../server/config');
const {errors, logging} = require('../../../../server/lib/common');
// Register an async handlebars helper for a given handlebars instance
function asyncHelperWrapper(hbs, name, fn) {
hbs.registerAsyncHelper(name, function returnAsync(context, options, cb) {
// Handle the case where we only get context and cb
if (!cb) {
cb = options;
options = undefined;
}
// Wrap the function passed in with a when.resolve so it can return either a promise or a value
Promise.resolve(fn.call(this, context, options)).then(function asyncHelperSuccess(result) {
cb(result);
}).catch(function asyncHelperError(err) {
var wrappedErr = err instanceof errors.GhostError ? err : new errors.IncorrectUsageError({
err: err,
context: 'registerAsyncThemeHelper: ' + name,
errorDetails: {
originalError: err
}
}),
result = config.get('env') === 'development' ? wrappedErr : '';
logging.error(wrappedErr);
cb(new hbs.SafeString(result));
});
});
}
// Register a handlebars helper for themes
module.exports.registerThemeHelper = function registerThemeHelper(name, fn) {
hbs.registerHelper(name, fn);
};
// Register an async handlebars helper for themes
module.exports.registerAsyncThemeHelper = function registerAsyncThemeHelper(name, fn) {
asyncHelperWrapper(hbs, name, fn);
};