Search in sources :

Example 6 with Template

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);
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStream(java.io.OutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) Writable(groovy.lang.Writable) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) MimeTypes(ratpack.file.MimeTypes) ByteBuf(io.netty.buffer.ByteBuf) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) MarkupTemplate(ratpack.groovy.template.MarkupTemplate) Template(groovy.text.Template)

Example 7 with Template

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;
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException) IOException(java.io.IOException) SimpleTemplateEngine(groovy.text.SimpleTemplateEngine) Template(groovy.text.Template)

Example 8 with Template

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;
    }
}
Also used : IOException(java.io.IOException) Template(groovy.text.Template)

Example 9 with Template

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());
}
Also used : TestExplanation1(eu.esdihumboldt.hale.common.align.model.impl.mdexpl.test.TestExplanation1) Template(groovy.text.Template) Test(org.junit.Test)

Example 10 with Template

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);
    }
}
Also used : Writable(groovy.lang.Writable) Template(groovy.text.Template)

Aggregations

Template (groovy.text.Template)41 IOException (java.io.IOException)19 HashMap (java.util.HashMap)7 ServletException (javax.servlet.ServletException)7 Writable (groovy.lang.Writable)6 SimpleTemplateEngine (groovy.text.SimpleTemplateEngine)6 Writer (java.io.Writer)6 File (java.io.File)5 StringWriter (java.io.StringWriter)3 URL (java.net.URL)3 GroovyObject (groovy.lang.GroovyObject)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 GrailsTagException (org.grails.taglib.GrailsTagException)2 Test (org.junit.Test)2 WebConfig (com.haulmont.cuba.web.WebConfig)1 TestExplanation1 (eu.esdihumboldt.hale.common.align.model.impl.mdexpl.test.TestExplanation1)1