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