Went back to a happy medium of density.
This commit is contained in:
@@ -88,10 +88,9 @@ Example:
|
|||||||
}
|
}
|
||||||
|
|
||||||
String tmpl = "{{#persons}}{{name}}: {{age}}{{/persons}}\n";
|
String tmpl = "{{#persons}}{{name}}: {{age}}{{/persons}}\n";
|
||||||
Object ctx = new Object() {
|
Mustache.compiler().compile(tmpl).execute(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));
|
||||||
};
|
});
|
||||||
Mustache.compiler().compile(tmpl).execute(ctx, new OutputStreamWriter(System.out)));
|
|
||||||
|
|
||||||
// result:
|
// result:
|
||||||
// Elvis: 75
|
// Elvis: 75
|
||||||
@@ -125,10 +124,9 @@ 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:
|
||||||
|
|
||||||
Object ctx = new Object() {
|
Mustache.compiler().escapeHTML(false).compile("{{foo}}").execute(new Object() {
|
||||||
String foo = "<bar>";
|
String foo = "<bar>";
|
||||||
};
|
});
|
||||||
Mustache.compiler().escapeHTML(false).compile("{{foo}}").execute(ctx);
|
|
||||||
// result: <bar>
|
// result: <bar>
|
||||||
// not: <bar>
|
// not: <bar>
|
||||||
|
|
||||||
@@ -141,10 +139,9 @@ 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
|
||||||
Object ctx = new Object() {
|
Mustache.compiler().compile("{{#names}}{{this}}{/names}}").execute(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
|
||||||
@@ -159,10 +156,9 @@ One will often make use of these special variables in an inverted section, as
|
|||||||
follows:
|
follows:
|
||||||
|
|
||||||
String tmpl = "{{#things}}{{^-first}}, {{/-first}}{{self}}{{/things}}";
|
String tmpl = "{{#things}}{{^-first}}, {{/-first}}{{self}}{{/things}}";
|
||||||
Object ctx = new Object() {
|
Mustache.compiler().compile(tmpl).execute(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
|
||||||
@@ -177,10 +173,9 @@ 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.
|
||||||
|
|
||||||
String tmpl = "My favorite things:\n{{#things}}{{-index}}. {{self}}\n{{/things}}";
|
String tmpl = "My favorite things:\n{{#things}}{{-index}}. {{self}}\n{{/things}}";
|
||||||
Object ctx = new Object() {
|
Mustache.compiler().compile(tmpl).execute(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
|
||||||
@@ -194,12 +189,11 @@ 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:
|
||||||
|
|
||||||
Object ctx = new Object() {
|
Mustache.compiler().compile("Hello {{field.who}}!").execute(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:
|
||||||
@@ -244,11 +238,10 @@ 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}}";
|
||||||
Object ctx = new Object() {
|
Mustache.compiler().compile(template).execute(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
|
||||||
|
|||||||
Reference in New Issue
Block a user