Skip to content

Commit a4199a5

Browse files
committed
feat: Warn users if we do not override helpers in templates
1 parent 38dbd52 commit a4199a5

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

lib/utils.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ function renderTemplate({template, templateHelpers, data}) {
1919
var forEach = require('lodash/collection/forEach');
2020
var content;
2121

22+
// We add all our template helper methods to the template as lambdas. Note
23+
// that lambdas in Mustache are supposed to accept a second argument of
24+
// `render` to get the rendered value, not the literal `{{value}}`. But
25+
// this is currently broken (see
26+
// https://github.com/twitter/hogan.js/issues/222).
27+
function addTemplateHelpersToData(templateData) {
28+
templateData.helpers = {};
29+
forEach(templateHelpers, (method, name) => {
30+
data.helpers[name] = () => {
31+
return (value) => {
32+
return method(hogan.compile(value).render(templateData));
33+
};
34+
};
35+
});
36+
}
37+
2238
if (typeof template !== 'string' && typeof template !== 'function') {
2339
throw new Error('Template must be `string` or `function`');
2440
}
@@ -28,20 +44,13 @@ function renderTemplate({template, templateHelpers, data}) {
2844
}
2945

3046
if (typeof template === 'string') {
31-
// We add all our template helper methods to the template as lambdas. Note
32-
// that lambdas in Mustache are supposed to accept a second argument of
33-
// `render` to get the rendered value, not the literal `{{value}}`. But
34-
// this is currently broken (see
35-
// https://github.com/twitter/hogan.js/issues/222).
36-
data.helpers = {};
37-
forEach(templateHelpers, (method, name) => {
38-
data.helpers[name] = () => {
39-
return (value) => {
40-
return method(hogan.compile(value).render(data));
41-
};
42-
};
43-
});
44-
content = hogan.compile(template).render(data);
47+
if (!data.helpers) {
48+
data = addTemplateHelpersToData(data);
49+
} else {
50+
console.warn('TemplateHelpers not included because template data already contained a .helpers key');
51+
}
52+
53+
content = hogan.compile(this.props.template).render(data);
4554
}
4655

4756
return content;

0 commit comments

Comments
 (0)