added MustacheCustomContext
This commit is contained in:
@@ -35,6 +35,7 @@ public abstract class BasicCollector implements Mustache.Collector
|
||||
}
|
||||
|
||||
public Mustache.VariableFetcher createFetcher (Object ctx, String name) {
|
||||
if (ctx instanceof MustacheCustomContext) return CUSTOM_FETCHER;
|
||||
if (ctx instanceof Map<?,?>) return MAP_FETCHER;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
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() {
|
||||
public Object get (Object ctx, String name) throws Exception {
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user