protected Template loadTemplate(String name) { try { Reader template; template = this.mustache.loader.getTemplate(name); return this.mustache.compile(template); } catch (Exception ex) { throw new IllegalStateException("Cannot load template " + name, ex); } }
@Override protected Mustache.Compiler createViewFactory(ResourceLoader templateLoader) { return Mustache.compiler().withLoader(loader(templateLoader)); }
@Override protected Mono<Void> renderInternal(Map<String, Object> model, MediaType contentType, ServerWebExchange exchange) { Resource resource = resolveResource(); if (resource == null) { return Mono.error(new IllegalStateException( "Could not find Mustache template with URL [" + getUrl() + "]")); } DataBuffer dataBuffer = exchange.getResponse().bufferFactory().allocateBuffer(); try (Reader reader = getReader(resource)) { Template template = this.compiler.compile(reader); Charset charset = getCharset(contentType).orElse(getDefaultCharset()); try (Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(), charset)) { template.execute(model, writer); writer.flush(); } } catch (Exception ex) { DataBufferUtils.release(dataBuffer); return Mono.error(ex); } return exchange.getResponse().writeWith(Flux.just(dataBuffer)); }
protected View view(final Template mustache) { return (model, out) -> { PrintWriter writer = new PrintWriter(out); mustache.execute(model, writer); writer.flush(); }; } }
@Override public View getView(String viewName, final ResourceLoader resourceLoader) throws Exception { String filename = filename(viewName); byte[] bytes = resourceLoader.load(filename); if (bytes == null) return null; Mustache.TemplateLoader loader = loader(resourceLoader); String template = new String(bytes); Mustache.Compiler compiler = getViewFactory(resourceLoader); Template mustache = compiler.withLoader(loader).compile(template); return view(mustache); }
/** * Create a {@code MustacheViewResolver} backed by a default instance of a * {@link Compiler}. */ public MustacheViewResolver() { this.compiler = Mustache.compiler(); setViewClass(requiredViewClass()); }
private Template createTemplate(Resource resource) throws IOException { try (Reader reader = getReader(resource)) { return this.compiler.compile(reader); } }
@Override public void execute (Template tmpl, Template.Context ctx, Writer out) { Object value = tmpl.getValueOrDefault(ctx, _name, _line); if (value == null) { String msg = Template.isThisName(_name) ? "Resolved '.' to null (which is disallowed), on line " + _line : "No key, method or field with name '" + _name + "' on line " + _line; throw new MustacheException.Context(msg, _name, _line); } write(out, _escaper.escape(_formatter.format(value))); } @Override public void decompile (Delims delims, StringBuilder into) {
@ModelAttribute("linkTo") public Mustache.Lambda linkTo() { return (frag, out) -> out.write(this.getLinkTo().apply(frag.execute())); }
@Override public void visit (Visitor visitor) { if (visitor.visitInclude(_name)) { getTemplate().visit(visitor); } } protected Template getTemplate () {
/** * Returns a compiler that escapes HTML by default and does not use standards mode. */ public static Compiler compiler () { return new Compiler(/*standardsMode=*/false, /*strictSections=*/false, /*nullValue=*/null, /*missingIsNull=*/false, /*emptyStringIsFalse=*/false, /*zeroIsFalse=*/false, DEFAULT_FORMATTER, Escapers.HTML, FAILING_LOADER, new DefaultCollector(), new Delims()); }
/** Returns a compiler configured to use the supplied delims as default delimiters. * @param delims a string of the form {@code AB CD} or {@code A D} where A and B are * opening delims and C and D are closing delims. */ public Compiler withDelims (String delims) { return new Compiler(this.standardsMode, this.strictSections, this.nullValue, this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse, this.formatter, this.escaper, this.loader, this.collector, new Delims().updateDelims(delims)); }
public String process(String name, Map<String, ?> model) { try { Template template = getTemplate(name); return template.execute(model); } catch (Exception ex) { log.error("Cannot render: " + name, ex); throw new IllegalStateException("Cannot render template", ex); } }
private static Compiler mustacheCompiler() { return Mustache.compiler().withLoader(mustacheTemplateLoader()); }
/** * Create a {@code MustacheViewResolver} backed by a default instance of a * {@link Compiler}. */ public MustacheViewResolver() { this.compiler = Mustache.compiler(); setViewClass(requiredViewClass()); }
@Override protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { Template template = createTemplate( getApplicationContext().getResource(this.getUrl())); if (template != null) { template.execute(model, response.getWriter()); } }