Permalink
Browse files

Test and implementation for using dot notation in templates.

  • Loading branch information...
1 parent 88dbb49 commit dd3329f4b8c48dc25661a28fe7b834644e3a3f93 @pepve pepve committed May 9, 2012
View
@@ -107,10 +107,22 @@ function normalize(context, name, body) {
function walkToFind(context, name) {
var i = context.length;
+ var names = name.split('.');
+ var current;
+
+ walk:
while (i--) {
- if (name in context[i]) {
- return context[i][name];
+ current = context[i];
+
+ for (var j = 0; j < names.length; j++) {
+ if (names[j] in current) {
+ current = current[names[j]];
+ } else {
+ continue walk;
+ }
}
+
+ return current;
}
return undefined;
@@ -0,0 +1,5 @@
+Hello {{person.name}}
+You have just won ${{price.value}}!
+{{#person.in_ca}}
+Well, ${{ states.ca.taxed_value }}, after taxes.
+{{/person.in_ca}}
@@ -0,0 +1,16 @@
+{
+ person: {
+ name: "Chris",
+ in_ca: true
+ },
+ price: {
+ value: 10000
+ },
+ states: {
+ ca: {
+ taxed_value: function () {
+ return this.price.value - (this.price.value * 0.4);
+ }
+ }
+ }
+}
@@ -0,0 +1,5 @@
+Hello Chris
+You have just won $10000!
+
+Well, $6000, after taxes.
+
@@ -20,6 +20,7 @@ mu.root = path.join(__dirname, 'examples');
'recursion_with_same_names',
'reuse_of_enumerables',
'simple',
+ 'dot_notation',
'twice',
'two_in_a_row',
'unescaped'

0 comments on commit dd3329f

Please sign in to comment.