Search in sources :

Example 11 with PebbleTemplate

use of com.mitchellbosecke.pebble.template.PebbleTemplate in project Skree by Skelril.

the class LoaderRegistry method renderTemplate.

private String renderTemplate(String templateContent) throws PebbleException, IOException {
    PebbleTemplate compiledTemplate = TEMPLATE_ENGINE.getTemplate(templateContent);
    Map<String, Object> context = new HashMap<>();
    for (Map.Entry<String, String> entry : constants.entrySet()) {
        String constant = entry.getKey();
        String value = entry.getValue();
        context.put(constant, value);
    }
    try (Writer writer = new StringWriter()) {
        compiledTemplate.evaluate(writer, context);
        return writer.toString();
    }
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) PebbleTemplate(com.mitchellbosecke.pebble.template.PebbleTemplate) Map(java.util.Map) HashMap(java.util.HashMap) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 12 with PebbleTemplate

use of com.mitchellbosecke.pebble.template.PebbleTemplate in project vertx-web by vert-x3.

the class PebbleTemplateEngineImpl method render.

@Override
public void render(Map<String, Object> context, String templateFile, Handler<AsyncResult<Buffer>> handler) {
    try {
        String src = adjustLocation(templateFile);
        TemplateHolder<PebbleTemplate> template = getTemplate(src);
        if (template == null) {
            // real compile
            synchronized (this) {
                template = new TemplateHolder<>(pebbleEngine.getTemplate(adjustLocation(src)));
            }
            putTemplate(src, template);
        }
        // special key for lang selection
        final String lang = (String) context.get("lang");
        // rendering
        final StringWriter stringWriter = new StringWriter();
        template.template().evaluate(stringWriter, context, lang == null ? Locale.getDefault() : Locale.forLanguageTag(lang));
        handler.handle(Future.succeededFuture(Buffer.buffer(stringWriter.toString())));
    } catch (final Exception ex) {
        handler.handle(Future.failedFuture(ex));
    }
}
Also used : StringWriter(java.io.StringWriter) PebbleTemplate(com.mitchellbosecke.pebble.template.PebbleTemplate)

Example 13 with PebbleTemplate

use of com.mitchellbosecke.pebble.template.PebbleTemplate in project vertx-web by vert-x3.

the class PebbleTemplateEngineImpl method render.

@Override
public void render(RoutingContext context, String templateDirectory, String templateFileName, Handler<AsyncResult<Buffer>> handler) {
    try {
        templateFileName = templateDirectory + templateFileName;
        PebbleTemplate template = isCachingEnabled() ? cache.get(templateFileName) : null;
        if (template == null) {
            // real compile
            final String loc = adjustLocation(templateFileName);
            template = pebbleEngine.getTemplate(loc);
            if (isCachingEnabled()) {
                cache.put(templateFileName, template);
            }
        }
        final Locale locale = context.acceptableLanguages().stream().findFirst().map(LanguageHeader::value).map(Locale::forLanguageTag).orElseGet(Locale::getDefault);
        final Map<String, Object> variables = new HashMap<>(1);
        variables.put("context", context);
        // Pass defined variables in context to engine
        variables.putAll(context.data());
        final StringWriter stringWriter = new StringWriter();
        template.evaluate(stringWriter, variables, locale);
        handler.handle(Future.succeededFuture(Buffer.buffer(stringWriter.toString())));
    } catch (final Exception ex) {
        handler.handle(Future.failedFuture(ex));
    }
}
Also used : Locale(java.util.Locale) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) PebbleTemplate(com.mitchellbosecke.pebble.template.PebbleTemplate) LanguageHeader(io.vertx.ext.web.LanguageHeader)

Example 14 with PebbleTemplate

use of com.mitchellbosecke.pebble.template.PebbleTemplate in project symja_android_library by axkr.

the class BarTrace method asJavascript.

@Override
public String asJavascript(int i) {
    Writer writer = new StringWriter();
    PebbleTemplate compiledTemplate;
    try {
        compiledTemplate = engine.getTemplate("trace_template.html");
        compiledTemplate.evaluate(writer, getContext(i));
    } catch (PebbleException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return writer.toString();
}
Also used : PebbleException(com.mitchellbosecke.pebble.error.PebbleException) StringWriter(java.io.StringWriter) PebbleTemplate(com.mitchellbosecke.pebble.template.PebbleTemplate) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 15 with PebbleTemplate

use of com.mitchellbosecke.pebble.template.PebbleTemplate in project symja_android_library by axkr.

the class ContourTrace method asJavascript.

@Override
public String asJavascript(int i) {
    Writer writer = new StringWriter();
    PebbleTemplate compiledTemplate;
    try {
        compiledTemplate = engine.getTemplate("trace_template.html");
        compiledTemplate.evaluate(writer, getContext());
    } catch (PebbleException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return writer.toString();
}
Also used : PebbleException(com.mitchellbosecke.pebble.error.PebbleException) StringWriter(java.io.StringWriter) PebbleTemplate(com.mitchellbosecke.pebble.template.PebbleTemplate) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

PebbleTemplate (com.mitchellbosecke.pebble.template.PebbleTemplate)20 StringWriter (java.io.StringWriter)19 Writer (java.io.Writer)17 PebbleException (com.mitchellbosecke.pebble.error.PebbleException)15 IOException (java.io.IOException)15 UncheckedIOException (java.io.UncheckedIOException)15 HashMap (java.util.HashMap)2 Utils.dataAsString (tech.tablesaw.plotly.Utils.dataAsString)2 PebbleEngine (com.mitchellbosecke.pebble.PebbleEngine)1 LanguageHeader (io.vertx.ext.web.LanguageHeader)1 Locale (java.util.Locale)1 Map (java.util.Map)1