BorisMoore / jquery-tmpl

The original official jQuery Templates plugin. This project was maintained by the jQuery team as an official jQuery plugin. It is no longer in active development, and has been superseded by JsRender.
3.23k stars 1.01k forks source link

Tmpl fails when a nested data object is null or undefined #156

Closed eirikwang closed 13 years ago

eirikwang commented 13 years ago

Given a data structure of: { nested :{value:value}, nullNested: {value:value}

When the nullNested is null or undefined, tmpl throws exception (line 361 jQuery.tmpl.js, beta1 version)

Code to reproduce: var nullCase = jQuery.tmpl("${nested.value} ${nullNested.value}", {"nested":{"value":"value"}, "nullNested":null}); var undefinedCase = jQuery.tmpl("${nested.value} ${nullNested.value}", {"nested":{"value":"value"}});

eirikwang commented 13 years ago

Latest version from GIT has this problem as well.

BorisMoore commented 13 years ago

Try using $data.nullNested... But if you do paths a.b.c.d the template engine will not test for undefined all the way down the chain, nor does it do a try catch. Perf would be impacted if it did...

eirikwang commented 13 years ago

Made a workaround, ensuring all nested objects are assigned "dummy" value if null.