Ghost/core/frontend/helpers/is.js
Hannah Wolfe 870bf27394
Removed use of i18n from helpers in favour of tpl
- i18n is an old pattern that failed
- we are slowly replacing i18n with the tpl helper
2021-09-28 11:42:59 +01:00

34 lines
874 B
JavaScript

// # Is Helper
// Usage: `{{#is "paged"}}`, `{{#is "index, paged"}}`
// Checks whether we're in a given context.
const {logging, tpl} = require('../services/proxy');
const _ = require('lodash');
const messages = {
invalidAttribute: 'Invalid or no attribute given to is helper'
};
module.exports = function is(context, options) {
options = options || {};
const currentContext = options.data.root.context;
if (!_.isString(context)) {
logging.warn(tpl(messages.invalidAttribute));
return;
}
function evaluateContext(expr) {
return expr.split(',').map(function (v) {
return v.trim();
}).reduce(function (p, c) {
return p || _.includes(currentContext, c);
}, false);
}
if (evaluateContext(context)) {
return options.fn(this);
}
return options.inverse(this);
};