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();
}
}
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));
}
}
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));
}
}
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();
}
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();
}
Aggregations