Ghost/core/frontend/services/themes/handlebars/template.js
Daniel Lockyer 40064a395a Switched frontend i18n requires to go through proxy
- we export i18n from `core/frontend/services/proxy` and this is used in
  the most of the places in the frontend code
- this commit aligns the rest of the code in core/frontend to use the
  proxy too
- unfortunately core/frontend/services/themes/i18n.js loops back to the
  proxy so we have a circular dependency
2020-11-26 14:00:28 +00:00

33 lines
1.1 KiB
JavaScript

// ## Template utils
const templates = {};
const _ = require('lodash');
const errors = require('@tryghost/errors');
const hbs = require('../engine');
const {i18n} = require('../../proxy');
// Execute a template helper
// All template helpers are register as partial view.
templates.execute = function execute(name, context, data) {
const partial = hbs.handlebars.partials[name];
if (partial === undefined) {
throw new errors.IncorrectUsageError({
message: i18n.t('warnings.helpers.template.templateNotFound', {name: name})
});
}
// If the partial view is not compiled, it compiles and saves in handlebars
if (typeof partial === 'string') {
hbs.registerPartial(partial);
}
return new hbs.SafeString(partial(context, data));
};
templates.asset = _.template('<%= source %>?v=<%= version %>');
templates.link = _.template('<a href="<%= url %>"><%= text %></a>');
templates.script = _.template('<script src="<%= source %>?v=<%= version %>"></script>');
templates.input = _.template('<input class="<%= className %>" type="<%= type %>" name="<%= name %>" <%= extras %> />');
module.exports = templates;