In case @ mblase75 is right (read: this is impossible) and no one else comes up with anything else, here is the workaround I came up with.
JsFiddle example
Since tmpl previously jumps level to level into arrays, I just came up with a system of templates that bounce from each other as the array appears. One template only processes array elements, allowing nested objects / arrays nested in it (it seems). Duplication is a little rough, but it seems to do the job even for crazy nested arrays.
HTML
<div class="results"></div> <script id="arrayDisplayTemplate" type="text/x-jquery-tmpl"> <li> {{if null !== $data && "object" === typeof ($data)}} {{if $data instanceof Array}} [ <ul> {{tmpl($data, { formatDisplay: $item.formatDisplay }) "#arrayItemTemplate"}} </ul> ] {{else}} {{tmpl($data, { formatDisplay: $item.formatDisplay }) "#reflectTemplate"}} {{/if}} {{else}} ${$item.formatDisplay($data)} {{/if}} </li> </script> <script id="reflectTemplate" type="text/x-jquery-tmpl"> <ul> {{each(i, prop) $data}} {{if $data.hasOwnProperty(i)}} <li> ${i}: {{if null !== prop && "object" === typeof (prop)}} {{if prop instanceof Array}} [ <ul> {{tmpl(prop, { formatDisplay: $item.formatDisplay }) "#arrayDisplayTemplate"}} </ul> ] {{else}} {{tmpl(prop, { formatDisplay: $item.formatDisplay }) "#reflectTemplate"}} {{/if}} {{else}} ${$item.formatDisplay(prop)} {{/if}} </li> {{/if}} {{/each}} </ul> </script>
Javascript
var data = { test1: 123, test2: { w: [ "some", "string", "array" ], x: 1, y: 2, z: "abc" }, test3: [ "abc", "123", "def", "456" ], test4: null }, templateFunctions = { formatDisplay: function(propertyValue) { var propertyType = typeof (propertyValue), result = propertyValue; if (null === result) { result = "null"; } else if ("string" === propertyType) { result = "\"" + result + "\""; } return result; } }; $("#reflectTemplate").tmpl(data, templateFunctions).appendTo($(".results"));
source share