janl/mustache.js — Issue #159: Prevent name lookup to go up the stack
Author: jpfiset
Created: 2012-01-25 19:54:30+00:00
State: open

For nested structures with keys that are repeated at multiple levels, the feature where a name lookup matches any context up the stack prevents the ability to scrutinize the nested structure.

For example, an object:

{
type: 'resource'
,components: [
{
type: 'email'
,address: 'abc@123.com'
}
,{
content: 'abc'
}
]
};

and a template:
{{#components}}{{#type}}{{type}}{{/type}}{{^type}}{{/type}},{{/components}}

The expected result would be:

email,,

but mustache returns:

email,resource,

The reason is that there is a collision between the 'type' key found in the inner structure and the one found in the outer structure. Currently, mustache does not have a way to prevent the name lookup from walking up the stack.

There are two solutions to this problem:
1. Add a special naming convention to prevent walking up the stack. For example, preceding names with a character such as '-'
2. Perform a walk up the stack only when a special naming convention is in use. For example, names that start with '+' would indicate that a stack lookup up is desired.