Added README.md and one extra test

- Try to make sure it's clear how to make interpolation work when working with handlebars helpers
This commit is contained in:
Hannah Wolfe 2021-09-30 16:17:49 +01:00
parent c9b4504bab
commit c886738bbc
2 changed files with 36 additions and 12 deletions

View File

@ -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).
Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE).

View File

@ -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 = {