Search in sources :

Example 1 with LanguageHeader

use of io.vertx.ext.web.LanguageHeader 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 2 with LanguageHeader

use of io.vertx.ext.web.LanguageHeader in project vertx-web by vert-x3.

the class ThymeleafTemplateEngineImpl method render.

@Override
public void render(RoutingContext context, String templateDirectory, String templateFileName, Handler<AsyncResult<Buffer>> handler) {
    templateFileName = templateDirectory + templateFileName;
    Buffer buffer = Buffer.buffer();
    try {
        Map<String, Object> data = new HashMap<>();
        data.put("context", context);
        data.putAll(context.data());
        synchronized (this) {
            templateResolver.setVertx(context.vertx());
            final List<LanguageHeader> acceptableLocales = context.acceptableLanguages();
            LanguageHeader locale = null;
            if (acceptableLocales.size() > 0) {
                // this is the users preferred locale
                locale = acceptableLocales.get(0);
            }
            templateEngine.process(templateFileName, new WebIContext(data, locale), new Writer() {

                @Override
                public void write(char[] cbuf, int off, int len) throws IOException {
                    buffer.appendString(new String(cbuf, off, len));
                }

                @Override
                public void flush() throws IOException {
                }

                @Override
                public void close() throws IOException {
                }
            });
        }
        handler.handle(Future.succeededFuture(buffer));
    } catch (Exception ex) {
        handler.handle(Future.failedFuture(ex));
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HashMap(java.util.HashMap) IOException(java.io.IOException) IOException(java.io.IOException) LanguageHeader(io.vertx.ext.web.LanguageHeader) Writer(java.io.Writer)

Example 3 with LanguageHeader

use of io.vertx.ext.web.LanguageHeader in project vertx-web by vert-x3.

the class TemplateHandlerImpl method handle.

@Override
public void handle(RoutingContext context) {
    String file = Utils.pathOffset(context.normalizedPath(), context);
    if (file.endsWith("/") && null != indexTemplate) {
        file += indexTemplate;
    }
    // however if there's no base strip / to avoid making the path absolute
    if (templateDirectory == null || "".equals(templateDirectory)) {
        // strip the leading slash from the filename
        file = file.substring(1);
    }
    // put the locale if present and not on the context yet into the context.
    if (!context.data().containsKey("lang")) {
        for (LanguageHeader acceptableLocale : context.acceptableLanguages()) {
            try {
                Locale.forLanguageTag(acceptableLocale.value());
            } catch (RuntimeException e) {
                // we couldn't parse the locale so it's not valid or unknown
                continue;
            }
            context.data().put("lang", acceptableLocale.value());
            break;
        }
    }
    // render using the engine
    engine.render(new JsonObject(context.data()), templateDirectory + file, res -> {
        if (res.succeeded()) {
            context.response().putHeader(HttpHeaders.CONTENT_TYPE, contentType).end(res.result());
        } else {
            context.fail(res.cause());
        }
    });
}
Also used : LanguageHeader(io.vertx.ext.web.LanguageHeader) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

LanguageHeader (io.vertx.ext.web.LanguageHeader)3 HashMap (java.util.HashMap)2 PebbleTemplate (com.mitchellbosecke.pebble.template.PebbleTemplate)1 Buffer (io.vertx.core.buffer.Buffer)1 JsonObject (io.vertx.core.json.JsonObject)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Locale (java.util.Locale)1