Search code examples
javascriptmustache

Mustache template namespace collision / scoping


I'm having a problem with what you might call namespace collision during the rendering of a mustache template.

Basically, the attribute "name" needs to be scoped to the array that the template is traversing and NOT the previous (parent) array.

Obviously the simple solution would be to change the name of the field, but I don't want to do that for what appears to be a bug. Am I missing something obvious here?

In the following fiddle, we should not be seeing the "name" attribute's list item being rendered in the nested list if members.name is undefined. Instead it is rendering families.name, and it shouldn't.

Example: http://jsfiddle.net/e3kMw/5/

<ul> {{#families}} <li>{{name}} <ul> {{#members}} {{#name}}<li>{{name}}</li>{{/name}} {{/members}} </ul> </li> {{/families}} </ul>


Solution

  • This is more like mustache/javascript weak typing colision. Set the value to an emply string instead of undefined

    {"name": ""}