Java Code Examples for groovy.text.Template#make()

The following examples show how to use groovy.text.Template#make() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroovyViewEngine.java    From ozark with Apache License 2.0 5 votes vote down vote up
public void processView(ViewEngineContext context) throws ViewEngineException {

        Map<String, Object> model = new HashMap<>(context.getModels().asMap());
        model.put("request", context.getRequest(HttpServletRequest.class));
        Charset charset = resolveCharsetAndSetContentType(context);
        try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset);
            InputStream resourceAsStream = servletContext.getResourceAsStream(resolveView(context));
            InputStreamReader in = new InputStreamReader(resourceAsStream, "UTF-8");) {
            Template template = markupTemplateEngine.createTemplate(in);
            Writable output = template.make(model);
            output.writeTo(writer);
        } catch (IOException | CompilationFailedException | ClassNotFoundException e) {
            throw new ViewEngineException(e);
        }
    }
 
Example 2
Source File: GroovyTemplateEngine.java    From pippo with Apache License 2.0 5 votes vote down vote up
@Override
public void renderString(String templateContent, Map<String, Object> model, Writer writer) {
    try {
        Template groovyTemplate = engine.createTemplate(templateContent);
        PippoGroovyTemplate gt = (PippoGroovyTemplate) groovyTemplate.make(model);
        gt.setup(getLanguages(), getMessages(), getRouter());
        gt.writeTo(writer);
    } catch (Exception e) {
        log.error("Error processing Groovy template {} ", templateContent, e);
        throw new PippoRuntimeException(e);
    }
}
 
Example 3
Source File: GroovyTemplateEngine.java    From jbake with MIT License 5 votes vote down vote up
@Override
public void renderDocument(final Map<String, Object> model, final String templateName, final Writer writer) throws RenderingException {
    try {
        Template template = findTemplate(templateName);
        Writable writable = template.make(wrap(model));
        writable.writeTo(writer);
    } catch (Exception e) {
        throw new RenderingException(e);
    }
}
 
Example 4
Source File: GroovyMarkupTemplateEngine.java    From jbake with MIT License 5 votes vote down vote up
@Override
public void renderDocument(final Map<String, Object> model, final String templateName, final Writer writer) throws RenderingException {
    try {
        Template template = templateEngine.createTemplateByPath(templateName);
        Map<String, Object> wrappedModel = wrap(model);
        Writable writable = template.make(wrappedModel);
        writable.writeTo(writer);
    } catch (Exception e) {
        throw new RenderingException(e);
    }
}
 
Example 5
Source File: CubaApplicationServlet.java    From cuba with Apache License 2.0 4 votes vote down vote up
protected String prepareErrorHtml(HttpServletRequest req, Throwable exception) {
    Messages messages = AppBeans.get(Messages.NAME);
    Configuration configuration = AppBeans.get(Configuration.NAME);

    WebConfig webConfig = configuration.getConfig(WebConfig.class);
    GlobalConfig globalConfig = configuration.getConfig(GlobalConfig.class);

    // SimpleTemplateEngine requires mutable map
    Map<String, Object> binding = new HashMap<>();
    binding.put("tryAgainUrl", "?restartApp");
    binding.put("productionMode", webConfig.getProductionMode());
    binding.put("messages", messages);
    binding.put("exception", exception);
    binding.put("exceptionName", exception.getClass().getName());
    binding.put("exceptionMessage", exception.getMessage());
    binding.put("exceptionStackTrace", ExceptionUtils.getStackTrace(exception));

    Locale locale = resolveLocale(req, messages, globalConfig);

    String serverErrorPageTemplatePath = webConfig.getServerErrorPageTemplate();

    String localeString = messages.getTools().localeToString(locale);
    String templateContent = getLocalizedTemplateContent(resources, serverErrorPageTemplatePath, localeString);
    if (templateContent == null) {
        templateContent = resources.getResourceAsString(serverErrorPageTemplatePath);

        if (templateContent == null) {
            throw new IllegalStateException("Unable to find server error page template " + serverErrorPageTemplatePath);
        }
    }

    SimpleTemplateEngine templateEngine = new SimpleTemplateEngine(getServletContext().getClassLoader());
    Template template = getTemplate(templateEngine, templateContent);

    Writable writable = template.make(binding);

    String html;
    try {
        html = writable.writeTo(new StringWriter()).toString();
    } catch (IOException e) {
        throw new RuntimeException("Unable to write server error page", e);
    }

    return html;
}