Added hasFeature email template helper (#20271)

refs
https://ghost.slack.com/archives/C06TQR9SHSM/p1716816764653789?thread_ts=1716795330.737919&cid=C06TQR9SHSM MOM-158

- adds a new `hasFeature` helper for adding content behind a flag within
Email Templates.
- Usage: `{{#hasFeature 'flagname'}} <p>html behind flag</p>
{{/hasFeature}}`
- Added additional testing for testing the helpers
This commit is contained in:
Ronald Langeveld 2024-05-29 11:43:46 +07:00 committed by GitHub
parent 7617759ae6
commit fb465e4704
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 205 additions and 44 deletions

View File

@ -10,6 +10,7 @@ const htmlToPlaintext = require('@tryghost/html-to-plaintext');
const tpl = require('@tryghost/tpl');
const cheerio = require('cheerio');
const {EmailAddressParser} = require('@tryghost/email-addresses');
const {registerHelpers} = require('./helpers/register-helpers');
const messages = {
subscriptionStatus: {
@ -755,53 +756,16 @@ class EmailRenderer {
return replacements;
}
getLabs() {
return this.#labs;
}
async renderTemplate(data) {
const labs = this.getLabs();
this.#handlebars = require('handlebars').create();
// Helpers
this.#handlebars.registerHelper('if', function (conditional, options) {
if (conditional) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
this.#handlebars.registerHelper('and', function () {
const len = arguments.length - 1;
for (let i = 0; i < len; i++) {
if (!arguments[i]) {
return false;
}
}
return true;
});
this.#handlebars.registerHelper('not', function () {
const len = arguments.length - 1;
for (let i = 0; i < len; i++) {
if (!arguments[i]) {
return true;
}
}
return false;
});
this.#handlebars.registerHelper('or', function () {
const len = arguments.length - 1;
for (let i = 0; i < len; i++) {
if (arguments[i]) {
return true;
}
}
return false;
});
// Register helpers
registerHelpers(this.#handlebars, labs);
// Partials
if (this.#labs.isSet('emailCustomization')) {

View File

@ -0,0 +1,55 @@
module.exports = {
registerHelpers(handlebars, labs) {
handlebars.registerHelper('if', function (conditional, options) {
if (conditional) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
handlebars.registerHelper('and', function () {
const len = arguments.length - 1;
for (let i = 0; i < len; i++) {
if (!arguments[i]) {
return false;
}
}
return true;
});
handlebars.registerHelper('not', function () {
const len = arguments.length - 1;
for (let i = 0; i < len; i++) {
if (!arguments[i]) {
return true;
}
}
return false;
});
handlebars.registerHelper('or', function () {
const len = arguments.length - 1;
for (let i = 0; i < len; i++) {
if (arguments[i]) {
return true;
}
}
return false;
});
handlebars.registerHelper('hasFeature', function (flag, options) {
if (labs.isSet(flag)) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
}
};

View File

@ -0,0 +1,142 @@
const assert = require('assert/strict');
const {registerHelpers} = require('../lib/helpers/register-helpers');
describe('registerHelpers', function () {
it('registers helpers', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return true;
}
};
registerHelpers(handlebars, labs);
assert.ok(handlebars.if);
assert.ok(handlebars.and);
assert.ok(handlebars.not);
assert.ok(handlebars.or);
assert.ok(handlebars.hasFeature);
});
it('if helper returns true', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return true;
}
};
registerHelpers(handlebars, labs);
const result = handlebars.if(true, {
fn: function () {
return true;
},
inverse: function () {
return false;
}
});
assert.equal(result, true);
});
it('if helper returns false', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return true;
}
};
registerHelpers(handlebars, labs);
const result = handlebars.if(false, {
fn: function () {
return true;
},
inverse: function () {
return false;
}
});
assert.equal(result, false);
});
it('and helper returns true', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return true;
}
};
registerHelpers(handlebars, labs);
const result = handlebars.and(true, true);
assert.equal(result, true);
});
it('usefeature helper returns true', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return true;
}
};
registerHelpers(handlebars, labs);
const result = handlebars.hasFeature('test', {
fn: function () {
return true;
},
inverse: function () {
return false;
}
});
assert.equal(result, true);
});
it('usefeature helper returns false', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return false;
}
};
registerHelpers(handlebars, labs);
const result = handlebars.hasFeature('test', {
fn: function () {
return true;
},
inverse: function () {
return false;
}
});
assert.equal(result, false);
});
});