@@ -19,6 +19,22 @@ function renderTemplate({template, templateHelpers, data}) {
19
19
var forEach = require ( 'lodash/collection/forEach' ) ;
20
20
var content ;
21
21
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
+
22
38
if ( typeof template !== 'string' && typeof template !== 'function' ) {
23
39
throw new Error ( 'Template must be `string` or `function`' ) ;
24
40
}
@@ -28,20 +44,13 @@ function renderTemplate({template, templateHelpers, data}) {
28
44
}
29
45
30
46
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 ) ;
45
54
}
46
55
47
56
return content ;
0 commit comments