Ghost/core/frontend/helpers/tiers.js
Rishabh 665c30f255 Added new {{tiers}} theme helper
refs https://github.com/TryGhost/Team/issues/1004

- adds new `{{tiers}}` helper behind `multipleProducts` flag
- `{{tiers}}` outputs a string with list of tiers that have access to specific post when used in a post context in theme
- outputs empty string when used out of a post context and without access to `visibility` property
- uses tiers attached to post column for data
2022-03-04 18:22:59 +05:30

60 lines
1.9 KiB
JavaScript

// # Tiers Helper
// Usage: `{{tiers}}`, `{{tiers separator=' - ' prefix=' : ' suffix=''}}`
//
// Returns a string of the tiers with access to the post.
// By default, tiers are separated by commas.
const {labs} = require('../services/proxy');
const {SafeString, escapeExpression} = require('../services/rendering');
const isString = require('lodash/isString');
function tiers(options = {}) {
options = options || {};
options.hash = options.hash || {};
const separator = isString(options.hash.separator) ? options.hash.separator : ', ';
const lastSeparator = isString(options.hash.lastSeparator) ? options.hash.lastSeparator : ' and ';
const prefix = isString(options.hash.prefix) ? options.hash.prefix : '';
let suffix = isString(options.hash.suffix) ? options.hash.suffix : '';
let output = '';
let accessProductsList = this.tiers;
if (accessProductsList && accessProductsList.length > 0) {
const tierNames = accessProductsList.map((tier) => {
return escapeExpression(tier.name);
});
if (tierNames.length === 1) {
output = tierNames[0];
suffix = isString(options.hash.suffix) ? options.hash.suffix : ' tier';
} else {
suffix = isString(options.hash.suffix) ? options.hash.suffix : ' tiers';
const firsts = tierNames.slice(0, tierNames.length - 1);
const last = tierNames[tierNames.length - 1];
output = firsts.join(separator) + lastSeparator + last;
}
}
if (output) {
output = prefix + output + suffix;
}
return new SafeString(output);
}
module.exports = function productsLabsWrapper() {
let self = this;
let args = arguments;
return labs.enabledHelper({
flagKey: 'multipleProducts',
flagName: 'Tiers',
helperName: 'tiers',
helpUrl: 'https://ghost.org/docs/themes/'
}, () => {
return tiers.apply(self, args);
});
};