added MustacheCustomContext

This commit is contained in:
Devin Smith
2018-08-21 17:20:00 -05:00
parent 4533567303
commit 433e9677d4
4 changed files with 30 additions and 0 deletions
+1
View File
@@ -75,6 +75,7 @@ void executeTemplate (Reader template, Writer out, Map<String, String> data) {
The execution context can be any Java object. Variables will be resolved via the following The execution context can be any Java object. Variables will be resolved via the following
mechanisms: mechanisms:
* If the context is a `MustacheCustomContext`, `MustacheCustomContext.get` will be used.
* If the context is a `Map`, `Map.get` will be used. * If the context is a `Map`, `Map.get` will be used.
* If a non-void method with the same name as the variable exists, it will be called. * If a non-void method with the same name as the variable exists, it will be called.
* If a non-void method named (for variable `foo`) `getFoo` exists, it will be called. * If a non-void method named (for variable `foo`) `getFoo` exists, it will be called.
@@ -35,6 +35,7 @@ public abstract class BasicCollector implements Mustache.Collector
} }
public Mustache.VariableFetcher createFetcher (Object ctx, String name) { public Mustache.VariableFetcher createFetcher (Object ctx, String name) {
if (ctx instanceof MustacheCustomContext) return CUSTOM_FETCHER;
if (ctx instanceof Map<?,?>) return MAP_FETCHER; if (ctx instanceof Map<?,?>) return MAP_FETCHER;
// if the name looks like a number, potentially use one of our 'indexing' fetchers // if the name looks like a number, potentially use one of our 'indexing' fetchers
@@ -65,6 +66,17 @@ public abstract class BasicCollector implements Mustache.Collector
return null; return null;
} }
protected static final Mustache.VariableFetcher CUSTOM_FETCHER = new Mustache.VariableFetcher() {
public Object get (Object ctx, String name) throws Exception {
MustacheCustomContext custom = (MustacheCustomContext)ctx;
Object val = custom.get(name);
return val == null ? Template.NO_FETCHER_FOUND : val;
}
@Override public String toString () {
return "CUSTOM_FETCHER";
}
};
protected static final Mustache.VariableFetcher MAP_FETCHER = new Mustache.VariableFetcher() { protected static final Mustache.VariableFetcher MAP_FETCHER = new Mustache.VariableFetcher() {
public Object get (Object ctx, String name) throws Exception { public Object get (Object ctx, String name) throws Exception {
Map<?,?> map = (Map<?,?>)ctx; Map<?,?> map = (Map<?,?>)ctx;
@@ -0,0 +1,8 @@
package com.samskivert.mustache;
/**
* Provides a simple interface for callers to implement their own logic for contextual values.
*/
public interface MustacheCustomContext {
Object get(String name) throws Exception;
}
@@ -45,6 +45,15 @@ public class MustacheTest extends SharedTests
}); });
} }
@Test public void testMustacheCustomContext() {
test("bar", "{{foo}}", new MustacheCustomContext() {
@Override
public Object get(String name) {
return "foo".equals(name) ? "bar" : null;
}
});
}
public interface HasDefault { public interface HasDefault {
default String getFoo () { return "bar"; } default String getFoo () { return "bar"; }
} }