2013-11-27 21:45:01 -05:00
|
|
|
// ## Template utils
|
2020-03-30 21:23:02 +01:00
|
|
|
const templates = {};
|
|
|
|
const _ = require('lodash');
|
|
|
|
const errors = require('@tryghost/errors');
|
2020-04-08 19:02:31 +01:00
|
|
|
const hbs = require('../engine');
|
2020-11-25 12:33:44 +00:00
|
|
|
const {i18n} = require('../../proxy');
|
2013-11-27 21:45:01 -05:00
|
|
|
|
2013-12-01 18:31:55 -05:00
|
|
|
// Execute a template helper
|
|
|
|
// All template helpers are register as partial view.
|
2019-03-09 19:35:19 +00:00
|
|
|
templates.execute = function execute(name, context, data) {
|
2020-04-29 16:44:27 +01:00
|
|
|
const partial = hbs.handlebars.partials[name];
|
2013-11-27 21:45:01 -05:00
|
|
|
|
2013-12-01 18:31:55 -05:00
|
|
|
if (partial === undefined) {
|
2020-03-30 21:23:02 +01:00
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: i18n.t('warnings.helpers.template.templateNotFound', {name: name})
|
2016-10-06 14:27:35 +02:00
|
|
|
});
|
2013-12-01 18:31:55 -05:00
|
|
|
}
|
2013-11-27 21:45:01 -05:00
|
|
|
|
2013-12-01 18:31:55 -05:00
|
|
|
// If the partial view is not compiled, it compiles and saves in handlebars
|
|
|
|
if (typeof partial === 'string') {
|
2014-01-23 16:22:25 +00:00
|
|
|
hbs.registerPartial(partial);
|
2013-12-01 18:31:55 -05:00
|
|
|
}
|
2013-11-27 21:45:01 -05:00
|
|
|
|
2019-03-09 19:35:19 +00:00
|
|
|
return new hbs.SafeString(partial(context, data));
|
2013-11-27 21:45:01 -05:00
|
|
|
};
|
|
|
|
|
2017-04-04 17:07:35 +01:00
|
|
|
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 %> />');
|
|
|
|
|
2014-09-10 00:06:24 -04:00
|
|
|
module.exports = templates;
|