From c886738bbcbf4962fed624b0d0a12f28720ffce9 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 30 Sep 2021 16:17:49 +0100 Subject: [PATCH] Added README.md and one extra test - Try to make sure it's clear how to make interpolation work when working with handlebars helpers --- ghost/tpl/README.md | 18 ++++++++++++++++-- ghost/tpl/test/tpl.test.js | 30 ++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/ghost/tpl/README.md b/ghost/tpl/README.md index 9c294e13db..25067a6579 100644 --- a/ghost/tpl/README.md +++ b/ghost/tpl/README.md @@ -11,6 +11,20 @@ or ## Usage +``` +const tpl = require('@tryghost/tpl'); +messages = { + myError: 'Something terrible happened to {something}' +}; + +console.error(tpl(messages.myError, {something: 'The thing'})); +``` + +* Takes strings like 'Your site is now available on {url}' and interpolates them with passed in data +* Will ignore double or triple braces like {{get}} or {{{content}}} +* Can handle escaped braces e.g. \\\\{\\\\{{helpername}\\\\}\\\\} +* There's a simple bare minimum escaping needed to make {{{helpername}}} work with interpolation e.g. {\\\\{{helpername}}} + ## Develop @@ -34,6 +48,6 @@ Follow the instructions for the top-level repo. -# Copyright & License +# Copyright & License -Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE). \ No newline at end of file +Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE). diff --git a/ghost/tpl/test/tpl.test.js b/ghost/tpl/test/tpl.test.js index c3bf6df3b0..c4579de858 100644 --- a/ghost/tpl/test/tpl.test.js +++ b/ghost/tpl/test/tpl.test.js @@ -42,6 +42,26 @@ describe('tpl', function () { result.should.eql('{{#get}} helper took 500ms to complete'); }); + it('ignores 3 braces', function () { + const string = 'The {{{helperName}}} helper is not available.'; + const data = { + helperName: 'get', + totalMs: '500' + }; + let result = tpl(string, data); + result.should.eql('The {{{helperName}}} helper is not available.'); + }); + + it('has a simple bare minimum escaping needed', function () { + const string = 'The {\\{{helperName}}} helper is not available.'; + const data = { + helperName: 'get', + totalMs: '500' + }; + let result = tpl(string, data); + result.should.eql('The {{get}} helper is not available.'); + }); + it('Can handle escaped left braces', function () { const string = 'The \\{\\{{helperName}}} helper is not available.'; const data = { @@ -62,16 +82,6 @@ describe('tpl', function () { result.should.eql('The {{get}} helper is not available.'); }); - it('has a simple bare minimum escaping needed', function () { - const string = 'The {\\{{helperName}}} helper is not available.'; - const data = { - helperName: 'get', - totalMs: '500' - }; - let result = tpl(string, data); - result.should.eql('The {{get}} helper is not available.'); - }); - it('Returns a sensible error if data is missing', function () { const string = '{helperName} helper took {totalMs}ms to complete'; const data = {