From 267f144f4177b5b31730d1c9b92b62d345b2910e Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 28 Feb 2011 20:27:12 -0800 Subject: [PATCH] Eased up on the density in the examples a bit. --- README.md | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 228b3d1..1770ca5 100644 --- a/README.md +++ b/README.md @@ -87,9 +87,11 @@ Example: 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)); - }, new OutputStreamWriter(System.out))); + }; + Mustache.compiler().compile(tmpl).execute(ctx, new OutputStreamWriter(System.out))); // result: // Elvis: 75 @@ -123,9 +125,10 @@ Not escaping HTML by default 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 = ""; - }); + }; + Mustache.compiler().escapeHTML(false).compile("{{foo}}").execute(ctx); // result: // not: <bar> @@ -138,9 +141,10 @@ instead of one of its members. This is particularly useful when iterating over lists. Mustache.compiler().compile("{{this}}").execute("hello"); // returns: hello - Mustache.compiler().compile("{{#names}}{{this}}{/names}}").execute(new Object() { + Object ctx = new Object() { List names () { return Arrays.asList("Tom", "Dick", "Harry"); } - }); + }; + Mustache.compiler().compile("{{#names}}{{this}}{/names}}").execute(ctx); // result: TomDickHarry ### -first and -last @@ -154,9 +158,11 @@ times. One will often make use of these special variables in an inverted section, as follows: - Mustache.compiler().compile("{{#things}}{{^-first}}, {{/-first}}{{self}}{{/things}}").execute(new Object() { + String tmpl = "{{#things}}{{^-first}}, {{/-first}}{{self}}{{/things}}"; + Object ctx = new Object() { List things = Arrays.asList("one", "two", "three"); - }); + }; + Mustache.compiler().compile(tmpl).execute(ctx); // result: one, two, three 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 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 things = Arrays.asList("Peanut butter", "Pen spinning", "Handstands"); - } + }; + Mustache.compiler().compile(tmpl).execute(ctx); // result: // My favorite things: // 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 example: - Mustache.compiler().compile("Hello {{field.who}}!").execute(new Object() { + Object ctx = new Object() { public Object field = new Object() { public String who () { return "world"; } } - }); + }; + Mustache.compiler().compile("Hello {{field.who}}!").execute(ctx); // result: Hello world! 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: String template = "{{outer}}:\n{{#inner}}{{outer}}.{{this}}\n{{/inner}}"; - Mustache.compiler().compile(template).execute(new Object() { + Object ctx = new Object() { String outer = "foo"; List inner = Arrays.asList("bar", "baz", "bif"); - }); + }; + Mustache.compiler().compile(template).execute(ctx); // results: // foo: // foo.bar