use of groovy.text.Template in project ratpack by ratpack.
the class MarkupTemplateRenderer method render.
@Override
public void render(Context ctx, MarkupTemplate template) throws Exception {
String contentType = template.getContentType();
contentType = contentType == null ? ctx.get(MimeTypes.class).getContentType(template.getName()) : contentType;
try {
Template compiledTemplate = engine.createTemplateByPath(template.getName());
Writable boundTemplate = compiledTemplate.make(template.getModel());
ByteBuf byteBuf = byteBufAllocator.directBuffer();
try {
OutputStream outputStream = new ByteBufOutputStream(byteBuf);
Writer writer = new OutputStreamWriter(outputStream, CharsetUtil.encoder(StandardCharsets.UTF_8));
boundTemplate.writeTo(writer);
} catch (Exception e) {
byteBuf.release();
throw e;
}
ctx.getResponse().send(contentType, byteBuf);
} catch (IOException e) {
ctx.error(e);
}
}
use of groovy.text.Template in project cuba by cuba-platform.
the class ExceptionReportServiceBean method getTemplate.
protected String getTemplate(Map<String, Object> binding, String template) {
SimpleTemplateEngine templateEngine = new SimpleTemplateEngine(scripting.getClassLoader());
Template bodyTemplate;
try {
bodyTemplate = templateEngine.createTemplate(template);
} catch (Exception e) {
throw new RuntimeException("Unable to compile Groovy template", e);
}
String reportBody;
try {
reportBody = bodyTemplate.make(binding).writeTo(new StringWriter(0)).toString();
} catch (IOException e) {
throw new RuntimeException("Unable to write Groovy template content", e);
}
return reportBody;
}
use of groovy.text.Template in project hale by halestudio.
the class MarkdownCellExplanation method getExplanation.
@Override
protected String getExplanation(Cell cell, boolean html, ServiceProvider provider, Locale locale) {
Optional<Template> maybeTemplate = getTemplate(getDefaultMessageClass(), locale);
if (maybeTemplate.isPresent()) {
try {
Template template = maybeTemplate.get();
// process template
String explanation = template.make(createBinding(cell, html, provider, locale)).toString();
if (html) {
explanation = pegdown.markdownToHtml(explanation);
}
return explanation;
} catch (Exception e) {
log.error("Error generating cell explanation for function " + cell.getTransformationIdentifier(), e);
return null;
}
} else {
return null;
}
}
use of groovy.text.Template in project hale by halestudio.
the class MarkdownCellExplanationTest method testTemplate.
/**
* Test if different templates for different languages are retrieved
* properly.
*/
@Test
public void testTemplate() {
TestExplanation1 exp = new TestExplanation1();
Optional<Template> english = exp.getTemplate(TestExplanation1.class, Locale.ENGLISH);
assertTrue(english.isPresent());
assertEquals("I'm an English man", english.get().make().toString());
Optional<Template> german = exp.getTemplate(TestExplanation1.class, Locale.GERMAN);
assertTrue(german.isPresent());
assertEquals("Ich bin Deutscher", german.get().make().toString());
Optional<Template> germany = exp.getTemplate(TestExplanation1.class, Locale.GERMANY);
assertTrue(germany.isPresent());
assertEquals("Ich bin Deutscher", germany.get().make().toString());
}
use of groovy.text.Template in project coprhd-controller by CoprHD.
the class TemplateEngine method generateFileFromTemplate.
/**
* Call the template with the parameters and save the response to the outputFile
*/
public static void generateFileFromTemplate(File templateFile, File outputFile, Map<String, Object> parameters) {
try {
Template template = getTemplate(templateFile);
Writable finishedTemplate = template.make(parameters);
try {
IOUtils.copy(new ByteArrayInputStream(finishedTemplate.toString().getBytes()), new FileOutputStream(outputFile));
} catch (Exception e) {
throw new RuntimeException("Error writing to file " + outputFile, e);
}
} catch (Exception e) {
DocReporter.printError("Error whilst generating page from file " + outputFile + " from template " + templateFile);
DocReporter.printError(e.getMessage());
throw new RuntimeException("Unable to process template" + templateFile, e);
}
}
Aggregations