Eased up on the density in the examples a bit.

This commit is contained in:
Michael Bayne
2011-02-28 20:27:12 -08:00
parent 40b5a2b2c8
commit 267f144f41
+24 -14
View File
@@ -87,9 +87,11 @@ Example:
protected int _age; protected int _age;
} }
Mustache.compiler().compile("{{#persons}}{{name}}: {{age}}{{/persons}}\n").execute(new Object() { String tmpl = "{{#persons}}{{name}}: {{age}}{{/persons}}\n";
Object ctx = new Object() {
Object persons = Arrays.asList(new Person("Elvis", 75), new Person("Madonna", 52)); Object persons = Arrays.asList(new Person("Elvis", 75), new Person("Madonna", 52));
}, new OutputStreamWriter(System.out))); };
Mustache.compiler().compile(tmpl).execute(ctx, new OutputStreamWriter(System.out)));
// result: // result:
// Elvis: 75 // Elvis: 75
@@ -123,9 +125,10 @@ Not escaping HTML by default
You can change the default HTML escaping behavior when obtaining a compiler: You can change the default HTML escaping behavior when obtaining a compiler:
Mustache.compiler().escapeHTML(false).compile("{{foo}}").execute(new Object() { Object ctx = new Object() {
String foo = "<bar>"; String foo = "<bar>";
}); };
Mustache.compiler().escapeHTML(false).compile("{{foo}}").execute(ctx);
// result: <bar> // result: <bar>
// not: &lt;bar&gt; // not: &lt;bar&gt;
@@ -138,9 +141,10 @@ instead of one of its members. This is particularly useful when iterating over
lists. lists.
Mustache.compiler().compile("{{this}}").execute("hello"); // returns: hello Mustache.compiler().compile("{{this}}").execute("hello"); // returns: hello
Mustache.compiler().compile("{{#names}}{{this}}{/names}}").execute(new Object() { Object ctx = new Object() {
List<String> names () { return Arrays.asList("Tom", "Dick", "Harry"); } List<String> names () { return Arrays.asList("Tom", "Dick", "Harry"); }
}); };
Mustache.compiler().compile("{{#names}}{{this}}{/names}}").execute(ctx);
// result: TomDickHarry // result: TomDickHarry
### -first and -last ### -first and -last
@@ -154,9 +158,11 @@ times.
One will often make use of these special variables in an inverted section, as One will often make use of these special variables in an inverted section, as
follows: follows:
Mustache.compiler().compile("{{#things}}{{^-first}}, {{/-first}}{{self}}{{/things}}").execute(new Object() { String tmpl = "{{#things}}{{^-first}}, {{/-first}}{{self}}{{/things}}";
Object ctx = new Object() {
List<String> things = Arrays.asList("one", "two", "three"); List<String> things = Arrays.asList("one", "two", "three");
}); };
Mustache.compiler().compile(tmpl).execute(ctx);
// result: one, two, three // result: one, two, three
Note that the values of `-first` and `-last` refer only to the inner-most Note that the values of `-first` and `-last` refer only to the inner-most
@@ -170,9 +176,11 @@ section, 2 for the second, 3 for the third and so forth. It contains 0 at all
other times. Note that it also contains 0 for a section that is populated by a other times. Note that it also contains 0 for a section that is populated by a
singleton value rather than a list. singleton value rather than a list.
Mustache.compiler().compile("My favorite things:\n{{#things}}{{-index}}. {{self}}\n{{/things}}").execute(new Object() { String tmpl = "My favorite things:\n{{#things}}{{-index}}. {{self}}\n{{/things}}";
Object ctx = new Object() {
List<String> things = Arrays.asList("Peanut butter", "Pen spinning", "Handstands"); List<String> things = Arrays.asList("Peanut butter", "Pen spinning", "Handstands");
} };
Mustache.compiler().compile(tmpl).execute(ctx);
// result: // result:
// My favorite things: // My favorite things:
// 1. Peanut butter // 1. Peanut butter
@@ -186,11 +194,12 @@ In addition to resolving simple variables using the context, you can use
compound variables to extract data from sub-objects of the current context. For compound variables to extract data from sub-objects of the current context. For
example: example:
Mustache.compiler().compile("Hello {{field.who}}!").execute(new Object() { Object ctx = new Object() {
public Object field = new Object() { public Object field = new Object() {
public String who () { return "world"; } public String who () { return "world"; }
} }
}); };
Mustache.compiler().compile("Hello {{field.who}}!").execute(ctx);
// result: Hello world! // result: Hello world!
By taking advantage of reflection and bean-property-style lookups, you can do kooky things: By taking advantage of reflection and bean-property-style lookups, you can do kooky things:
@@ -235,10 +244,11 @@ If a variable is not found in a nested context, it is resolved in the next
outer context. This allows usage like the following: outer context. This allows usage like the following:
String template = "{{outer}}:\n{{#inner}}{{outer}}.{{this}}\n{{/inner}}"; String template = "{{outer}}:\n{{#inner}}{{outer}}.{{this}}\n{{/inner}}";
Mustache.compiler().compile(template).execute(new Object() { Object ctx = new Object() {
String outer = "foo"; String outer = "foo";
List<String> inner = Arrays.asList("bar", "baz", "bif"); List<String> inner = Arrays.asList("bar", "baz", "bif");
}); };
Mustache.compiler().compile(template).execute(ctx);
// results: // results:
// foo: // foo:
// foo.bar // foo.bar