Skip to content

Commit 5890cbf

Browse files
committed
Added dotted.name support for tags.
Based on Chris's commit 4dc4286
1 parent 895e741 commit 5890cbf

11 files changed

+52
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ Tags are always surrounded by mustaches like this `{{foobar}}`.
3737

3838
template = "{{say_hello}}, {{name}}"
3939

40+
### Dotted Tags
41+
Tags can also be nested to address object and array members in the context.
42+
43+
var view = {name: {first: "Chris", last: "Anderson"}, values: [1, 2]};
44+
template = "{{name.last}}, {{name.first}} {{values.0}}";
45+
4046
### Conditional Sections
4147
Conditional sections begin with `{{#condition}}` and end with `{{/condition}}`. When `condition` evaluates to true, the section is rendered, otherwise the hole block will output nothing at all. `condition` may be a function returning true/false or a simple boolean.
4248

examples/nested.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Yay {{person.name}}!
2+
Array style {{value.0}} {{value.1}}
3+
And {{more.nested.1.values}}!

examples/nested.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var nested = {
2+
person: {
3+
name: "Chris"
4+
},
5+
ohyeah: "awesome!",
6+
value: ["is", function() {return this.ohyeah;}],
7+
more: {
8+
nested: [29, {values: "kbai"}]
9+
}
10+
};

examples/nested.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Yay Chris!
2+
Array style is awesome!
3+
And kbai!
4+

examples/nested_array_bounds.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Out of bounds! {{foo.5}}

examples/nested_array_bounds.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var nested_array_bounds = {
2+
foo: [1, 2]
3+
};

examples/nested_array_bounds.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ERROR: 'foo.5' not found in context

examples/nested_error.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This doesn't exist: {{foo.bar}}

examples/nested_error.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var nested_error = {not_foo: {not_bar: "yay error!"}};

examples/nested_error.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ERROR: 'foo.bar' not found in context

mustache.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@ var Mustache = function() {
130130
*/
131131
find: function(name, context) {
132132
name = this.trim(name);
133-
if(typeof context[name] === "function") {
134-
return context[name].apply(context);
133+
var value = this.getValue(context, name);
134+
if(typeof value === "function") {
135+
return value.apply(context);
135136
}
136-
if(context[name] !== undefined) {
137-
return context[name];
137+
if(value !== undefined) {
138+
return value;
138139
}
139140
throw({message: "'" + name + "' not found in context"});
140141
},
@@ -157,6 +158,22 @@ var Mustache = function() {
157158
});
158159
},
159160

161+
getValue: function(context, name) {
162+
if(name == "." && context[name] != undefined) {
163+
return context[name];
164+
}
165+
var ctx = context;
166+
var parts = name.split(".");
167+
while(parts.length) {
168+
p = parts.shift();
169+
if(ctx[p] === undefined) {
170+
throw({message: "'" + name + "' not found in context"});
171+
}
172+
ctx = ctx[p];
173+
}
174+
return ctx
175+
},
176+
160177
/*
161178
Merges all properties of object `b` into object `a`.
162179
`b.property` overwrites a.property`

0 commit comments

Comments
 (0)