From 2c53104cf3f48e480502ace36f26653706b1116d Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 5 Mar 2018 09:27:18 -0800 Subject: [PATCH] Extract Compiler.loadTemplate helper. If a lambda wanted to load a template via its own template loader, this makes it a lot easier to do so. --- .../com/samskivert/mustache/Mustache.java | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index 5cd71fc..7904618 100644 --- a/src/main/java/com/samskivert/mustache/Mustache.java +++ b/src/main/java/com/samskivert/mustache/Mustache.java @@ -207,6 +207,33 @@ public class Mustache { (zeroIsFalse && (value instanceof Number) && ((Number)value).longValue() == 0); } + /** Loads and compiles the template {@code name} using this compiler's configured template + * loader. Note that this does no caching: the caller should cache the loaded template if + * they expect to use it multiple times. + * @return the compiled template. + * @throw MustacheException if the template could not be loaded (due to I/O exception) or + * compiled (due to syntax error, etc.). + */ + public Template loadTemplate (String name) throws MustacheException { + Reader tin = null; + try { + tin = loader.getTemplate(name); + return compile(tin); + } catch (Exception e) { + if (e instanceof RuntimeException) { + throw (RuntimeException)e; + } else { + throw new MustacheException("Unable to load template: " + name, e); + } + } finally { + if (tin != null) try { + tin.close(); + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } + } + } + protected Compiler (boolean standardsMode, boolean strictSections, String nullValue, boolean missingIsNull, boolean emptyStringIsFalse, boolean zeroIsFalse, Formatter formatter, Escaper escaper, TemplateLoader loader, @@ -800,23 +827,7 @@ public class Mustache { // we compile our template lazily to avoid infinie recursion if a template includes // itself (see issue #13) if (_template == null) { - Reader tin = null; - try { - tin = _comp.loader.getTemplate(_name); - _template = _comp.compile(tin); - } catch (Exception e) { - if (e instanceof RuntimeException) { - throw (RuntimeException)e; - } else { - throw new MustacheException("Unable to load template: " + _name, e); - } - } finally { - if (tin != null) try { - tin.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } - } + _template = _comp.loadTemplate(_name); } return _template; }