Search in sources :

Example 1 with Template

use of freemarker.template.Template in project dropwizard by dropwizard.

the class FreemarkerViewRenderer method render.

@Override
public void render(View view, Locale locale, OutputStream output) throws IOException {
    try {
        final Configuration configuration = configurationCache.getUnchecked(view.getClass());
        final Charset charset = view.getCharset().orElseGet(() -> Charset.forName(configuration.getEncoding(locale)));
        final Template template = configuration.getTemplate(view.getTemplateName(), locale, charset.name());
        template.process(view, new OutputStreamWriter(output, template.getEncoding()));
    } catch (Exception e) {
        throw new ViewRenderException(e);
    }
}
Also used : Configuration(freemarker.template.Configuration) ViewRenderException(io.dropwizard.views.ViewRenderException) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) ViewRenderException(io.dropwizard.views.ViewRenderException) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 2 with Template

use of freemarker.template.Template in project qi4j-sdk by Qi4j.

the class ValueCompositeResponseWriter method writeResponse.

@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
    if (result instanceof ValueComposite) {
        MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
        if (MediaType.APPLICATION_JSON.equals(type)) {
            StringRepresentation representation = new StringRepresentation(valueSerializer.serialize(result), MediaType.APPLICATION_JSON);
            response.setEntity(representation);
            return true;
        } else if (MediaType.TEXT_HTML.equals(type)) {
            Representation rep = new WriterRepresentation(MediaType.TEXT_HTML) {

                @Override
                public void write(Writer writer) throws IOException {
                    // Look for type specific template
                    Template template;
                    try {
                        template = cfg.getTemplate("/rest/template/" + result.getClass().getInterfaces()[0].getSimpleName() + ".htm");
                    } catch (Exception e) {
                        // Use default
                        template = cfg.getTemplate("value.htm");
                    }
                    Map<String, Object> context = new HashMap<String, Object>();
                    context.put("request", response.getRequest());
                    context.put("response", response);
                    context.put("result", result);
                    context.put("util", this);
                    try {
                        template.process(context, writer);
                    } catch (TemplateException e) {
                        throw new IOException(e);
                    }
                }

                public boolean isSequence(Object obj) {
                    return obj instanceof Collection;
                }
            };
            response.setEntity(rep);
            return true;
        }
    }
    return false;
}
Also used : TemplateException(freemarker.template.TemplateException) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) WriterRepresentation(org.restlet.representation.WriterRepresentation) IOException(java.io.IOException) ValueComposite(org.qi4j.api.value.ValueComposite) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) ResourceException(org.restlet.resource.ResourceException) Template(freemarker.template.Template) StringRepresentation(org.restlet.representation.StringRepresentation) WriterRepresentation(org.restlet.representation.WriterRepresentation) MediaType(org.restlet.data.MediaType) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer)

Example 3 with Template

use of freemarker.template.Template in project jfinal by jfinal.

the class FreeMarkerRender method render.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void render() {
    response.setContentType(getContentType());
    Map data = new HashMap();
    for (Enumeration<String> attrs = request.getAttributeNames(); attrs.hasMoreElements(); ) {
        String attrName = attrs.nextElement();
        data.put(attrName, request.getAttribute(attrName));
    }
    PrintWriter writer = null;
    try {
        Template template = config.getTemplate(view);
        writer = response.getWriter();
        // Merge the data-model and the template
        template.process(data, writer);
    } catch (Exception e) {
        throw new RenderException(e);
    } finally {
        if (writer != null)
            writer.close();
    }
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) TemplateException(freemarker.template.TemplateException) PrintWriter(java.io.PrintWriter) Template(freemarker.template.Template)

Example 4 with Template

use of freemarker.template.Template in project eweb4j-framework by laiweiwei.

the class FreemarkerRendererImpl method render.

public synchronized void render(Writer writer, Map<String, Object> datas) {
    if (datas == null)
        datas = new HashMap<String, Object>();
    try {
        String tplPath = paths.get(MVCConfigConstant.LAYOUT_SCREEN_CONTENT_KEY);
        // 将环境变量和输出部分结合
        if (this.layout != null) {
            for (Iterator<Entry<String, String>> it = this.paths.entrySet().iterator(); it.hasNext(); ) {
                Entry<String, String> e = it.next();
                String paramName = e.getKey();
                String path = e.getValue();
                StringWriter w = new StringWriter();
                Template template = cfg.getTemplate(path);
                template.setEncoding("UTF-8");
                template.process(datas, w);
                String screenContent = w.toString();
                datas.put(paramName, screenContent);
            }
            tplPath = layout;
        }
        Template template = cfg.getTemplate(tplPath);
        template.setEncoding("UTF-8");
        template.process(datas, writer);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Entry(java.util.Map.Entry) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Template(freemarker.template.Template)

Example 5 with Template

use of freemarker.template.Template in project eweb4j-framework by laiweiwei.

the class PetControlTest method testFreeMarker.

@Test
public void testFreeMarker() throws Exception {
    Configuration cfg = new Configuration();
    // 指定模板从何处加载的数据源,这里设置成一个文件目录。
    cfg.setDirectoryForTemplateLoading(new File("src/test/java/test/ftl"));
    // 指定模板如何检索数据模型
    cfg.setObjectWrapper(new DefaultObjectWrapper());
    Map root = new HashMap();
    root.put("user", "Big Joe");
    Map latest = new HashMap();
    root.put("latestProduct", latest);
    latest.put("url", "produces/greenmouse.html");
    latest.put("name", "green mouse");
    Template template = cfg.getTemplate("hello.html");
    Writer out = new OutputStreamWriter(System.out);
    template.process(root, out);
    out.flush();
}
Also used : Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Template(freemarker.template.Template) Test(org.junit.Test)

Aggregations

Template (freemarker.template.Template)72 IOException (java.io.IOException)33 StringWriter (java.io.StringWriter)32 Configuration (freemarker.template.Configuration)30 Writer (java.io.Writer)26 HashMap (java.util.HashMap)26 TemplateException (freemarker.template.TemplateException)23 OutputStreamWriter (java.io.OutputStreamWriter)13 File (java.io.File)9 Map (java.util.Map)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 JSONObject (org.json.JSONObject)6 SimpleHash (freemarker.template.SimpleHash)5 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)4 StringTemplateLoader (freemarker.cache.StringTemplateLoader)4 Environment (freemarker.core.Environment)4 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)4 BufferedWriter (java.io.BufferedWriter)4 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4