Ghost/core/server/helpers/template.js
Hannah Wolfe d2b1e0d4b7 Attr pass-thru & full context in partial helpers
refs #5162

- allow pagination and navigation partial helpers to have attributes passed through to them
    - e.g. {{navigation header=true}} -> {{#if header}} will now work
    - allows styling navigation to be done differently for different sections of the page
- properly create a data frame, and pass through "this" context
    - means {{navigation header=true}} is the same as {{> navigation header=true navigation=@site.navigation}}
    - our partial helpers, have the same behaviour exactly as if the partial was called directly
- this is additive, and improves behaviour
2019-03-09 21:21:01 +00:00

33 lines
1.1 KiB
JavaScript

var templates = {},
_ = require('lodash'),
proxy = require('./proxy'),
hbs = require('../services/themes/engine');
// ## Template utils
// Execute a template helper
// All template helpers are register as partial view.
templates.execute = function execute(name, context, data) {
var partial = hbs.handlebars.partials[name];
if (partial === undefined) {
throw new proxy.errors.IncorrectUsageError({
message: proxy.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;