Added basic lambda support.
This commit is contained in:
@@ -150,7 +150,19 @@ public class Mustache
|
||||
{
|
||||
/** Returns a reader for the template with the supplied name.
|
||||
* @throws Exception if the template could not be loaded for any reason. */
|
||||
public Reader getTemplate (String name) throws Exception;
|
||||
Reader getTemplate (String name) throws Exception;
|
||||
}
|
||||
|
||||
/** Used to handle lambdas. */
|
||||
public interface Lambda
|
||||
{
|
||||
/** Executes this lambda on the supplied template fragment. The lambda should write its
|
||||
* results to {@code out}.
|
||||
*
|
||||
* @param tmpl the fragment of the template that was passed to the lambda.
|
||||
* @param out the writer to which the lambda should write its output.
|
||||
*/
|
||||
void execute (Template.Fragment frag, Writer out) throws IOException;
|
||||
}
|
||||
|
||||
/** Used to read variables from values. */
|
||||
@@ -640,6 +652,12 @@ public class Mustache
|
||||
if ((Boolean)value) {
|
||||
executeSegs(tmpl, ctx, out);
|
||||
}
|
||||
} else if (value instanceof Lambda) {
|
||||
try {
|
||||
((Lambda)value).execute(tmpl.createFragment(_segs, ctx), out);
|
||||
} catch (IOException ioe) {
|
||||
throw new MustacheException(ioe);
|
||||
}
|
||||
} else if (_compiler.emptyStringIsFalse && "".equals(value)) {
|
||||
// omit the section
|
||||
} else {
|
||||
|
||||
@@ -36,6 +36,22 @@ import java.util.Map;
|
||||
*/
|
||||
public class Template
|
||||
{
|
||||
/**
|
||||
* Encapsulates a fragment of a template that is passed to a lambda. The fragment is bound to
|
||||
* the variable context that was in effect at the time the lambda was called.
|
||||
*/
|
||||
public abstract class Fragment {
|
||||
/** Executes this template fragment, writing its result to {@code out}. */
|
||||
public abstract void execute (Writer out);
|
||||
|
||||
/** Executes this template fragment and returns is result as a string. */
|
||||
public String execute () {
|
||||
StringWriter out = new StringWriter();
|
||||
execute(out);
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes this template with the given context, returning the results as a string.
|
||||
* @throws MustacheException if an error occurs while executing or writing the template.
|
||||
@@ -73,7 +89,7 @@ public class Template
|
||||
{
|
||||
_segs = segs;
|
||||
_compiler = compiler;
|
||||
_fcache = _compiler.collector.createFetcherCache();
|
||||
_fcache = compiler.collector.createFetcherCache();
|
||||
}
|
||||
|
||||
protected void executeSegs (Context ctx, Writer out) throws MustacheException
|
||||
@@ -83,6 +99,17 @@ public class Template
|
||||
}
|
||||
}
|
||||
|
||||
protected Fragment createFragment (final Segment[] segs, final Context ctx)
|
||||
{
|
||||
return new Fragment() {
|
||||
@Override public void execute (Writer out) {
|
||||
for (Segment seg : segs) {
|
||||
seg.execute(Template.this, ctx, out);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by executing segments to obtain the value of the specified variable in the supplied
|
||||
* context.
|
||||
|
||||
Reference in New Issue
Block a user