Explorar el Código

Added dotted.name support for tags.

Based on Chris's commit 4dc42864e0
with-js-extras
Paul J. Davis hace 16 años
padre
commit
5890cbf994
Se han modificado 11 ficheros con 52 adiciones y 4 borrados
  1. +6
    -0
      README.md
  2. +3
    -0
      examples/nested.html
  3. +10
    -0
      examples/nested.js
  4. +4
    -0
      examples/nested.txt
  5. +1
    -0
      examples/nested_array_bounds.html
  6. +3
    -0
      examples/nested_array_bounds.js
  7. +1
    -0
      examples/nested_array_bounds.txt
  8. +1
    -0
      examples/nested_error.html
  9. +1
    -0
      examples/nested_error.js
  10. +1
    -0
      examples/nested_error.txt
  11. +21
    -4
      mustache.js

+ 6
- 0
README.md Ver fichero

@@ -37,6 +37,12 @@ Tags are always surrounded by mustaches like this `{{foobar}}`.

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

### Dotted Tags
Tags can also be nested to address object and array members in the context.

var view = {name: {first: "Chris", last: "Anderson"}, values: [1, 2]};
template = "{{name.last}}, {{name.first}} {{values.0}}";

### Conditional Sections
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.



+ 3
- 0
examples/nested.html Ver fichero

@@ -0,0 +1,3 @@
Yay {{person.name}}!
Array style {{value.0}} {{value.1}}
And {{more.nested.1.values}}!

+ 10
- 0
examples/nested.js Ver fichero

@@ -0,0 +1,10 @@
var nested = {
person: {
name: "Chris"
},
ohyeah: "awesome!",
value: ["is", function() {return this.ohyeah;}],
more: {
nested: [29, {values: "kbai"}]
}
};

+ 4
- 0
examples/nested.txt Ver fichero

@@ -0,0 +1,4 @@
Yay Chris!
Array style is awesome!
And kbai!


+ 1
- 0
examples/nested_array_bounds.html Ver fichero

@@ -0,0 +1 @@
Out of bounds! {{foo.5}}

+ 3
- 0
examples/nested_array_bounds.js Ver fichero

@@ -0,0 +1,3 @@
var nested_array_bounds = {
foo: [1, 2]
};

+ 1
- 0
examples/nested_array_bounds.txt Ver fichero

@@ -0,0 +1 @@
ERROR: 'foo.5' not found in context

+ 1
- 0
examples/nested_error.html Ver fichero

@@ -0,0 +1 @@
This doesn't exist: {{foo.bar}}

+ 1
- 0
examples/nested_error.js Ver fichero

@@ -0,0 +1 @@
var nested_error = {not_foo: {not_bar: "yay error!"}};

+ 1
- 0
examples/nested_error.txt Ver fichero

@@ -0,0 +1 @@
ERROR: 'foo.bar' not found in context

+ 21
- 4
mustache.js Ver fichero

@@ -130,11 +130,12 @@ var Mustache = function() {
*/
find: function(name, context) {
name = this.trim(name);
if(typeof context[name] === "function") {
return context[name].apply(context);
var value = this.getValue(context, name);
if(typeof value === "function") {
return value.apply(context);
}
if(context[name] !== undefined) {
return context[name];
if(value !== undefined) {
return value;
}
throw({message: "'" + name + "' not found in context"});
},
@@ -157,6 +158,22 @@ var Mustache = function() {
});
},

getValue: function(context, name) {
if(name == "." && context[name] != undefined) {
return context[name];
}
var ctx = context;
var parts = name.split(".");
while(parts.length) {
p = parts.shift();
if(ctx[p] === undefined) {
throw({message: "'" + name + "' not found in context"});
}
ctx = ctx[p];
}
return ctx
},

/*
Merges all properties of object `b` into object `a`.
`b.property` overwrites a.property`


Cargando…
Cancelar
Guardar