Skip to content

Commit d63e274

Browse files
committed
Handle undefined variables in underscore templates
1 parent 19b2e21 commit d63e274

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

core/lib/pattern_engines/engine_underscore.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,24 @@
5353
// render it
5454
renderPattern: function renderPattern(template, data, partials) {
5555
var compiled = _.template(template);
56-
return compiled(_.extend(data, {
57-
_allData: data,
58-
_partials: partials
59-
}));
56+
var renderedHTML;
57+
58+
// This try-catch is necessary because references to undefined variables
59+
// in underscore templates are eval()ed directly as javascript, and as
60+
// such will throw very real exceptions that will shatter the whole build
61+
// process if we don't handle them.
62+
try {
63+
renderedHTML = compiled(_.extend(data || {}, {
64+
_allData: data,
65+
_partials: partials
66+
}));
67+
} catch (e) {
68+
var errorMessage = "WARNING: the underscore template " + template.fileName + " threw an exception; it probably just tried to reference an undefined variable. Is this pattern maybe missing a .json file?";
69+
console.log(errorMessage, e);
70+
renderedHTML = "<h1>Error in underscore template</h1>" + errorMessage;
71+
}
72+
73+
return renderedHTML;
6074
},
6175

6276
// registerPartial: function (oPattern) {

0 commit comments

Comments
 (0)