Question

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>

Was it helpful?

Solution

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

{"name": ""}

OTHER TIPS

Mustache intentionally searches ancestor scopes when it cannot find a requested key in the current scope. Unfortunately no good way around that other than transforming your model.

Edit:

You can see the relevant scope lookup code as posted in This SO answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top