/** * 将Map转为VelocityContext * * @param bindingMap 参数绑定的Map * @return {@link VelocityContext} */ private VelocityContext toContext(Map<?, ?> bindingMap) { return new VelocityContext(bindingMap); }
/** * Adds variables to the velocity context. */ public void add(final String name, final Object value) { this.context.put(name, value); }
VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); ve.init(); VelocityContext context = new VelocityContext(); context.put("date", getMyTimestampFunction()); Template t = ve.getTemplate( "templates/email_html_new.vm" ); StringWriter writer = new StringWriter(); t.merge( context, writer );
protected void processRecord(Schema schema, String headerTemplate, String sourceTemplate) { VelocityContext context = new VelocityContext(); context.put("schema", schema); context.put("StyleUtils", StyleUtils.class); context.put("TypeConverter", TypeConverter.class); context.put("namespacePrefix", namespacePrefix); StringWriter hdrWriter = new StringWriter(); engine.getTemplate(headerTemplate).merge(context, hdrWriter); appendResult(hdrWriter.toString(), true); StringWriter srcWriter = new StringWriter(); engine.getTemplate(sourceTemplate).merge(context, srcWriter); appendResult(srcWriter.toString(), false); }
/** * 融合模板和内容并写入到Writer * @param template 模板 * @param context 内容 * @param writer Writer */ public static void merge(Template template, VelocityContext context, Writer writer) { if(template == null) { throw new UtilException("Template is null"); } if(context == null) { context = new VelocityContext(globalContext); }else { //加入全局上下文 for (Entry<String, Object> entry : globalContext.entrySet()) { context.put(entry.getKey(), entry.getValue()); } } template.merge(context, writer); }
@Override public void writer(Map<String, Object> objectMap, String templatePath, String outputFile) throws Exception { if (StringUtils.isEmpty(templatePath)) { return; } Template template = velocityEngine.getTemplate(templatePath, ConstVal.UTF8); try (FileOutputStream fos = new FileOutputStream(outputFile); OutputStreamWriter ow = new OutputStreamWriter(fos, ConstVal.UTF8); BufferedWriter writer = new BufferedWriter(ow)) { template.merge(new VelocityContext(objectMap), writer); } logger.debug("模板:" + templatePath + "; 文件:" + outputFile); }
/** * Gets an instance of the velocity context * * Add the utility instances that are commonly used in many templates. * * @return the instance */ public static VelocityContext getInstance() { final VelocityContext context = new VelocityContext(); context.put("esc", new EscapeTool()); final WebUtils utils = new WebUtils(); context.put("utils", utils); return context; } }
private String generateSourceFromTemplate(String templateFullName, VelocityContext context) { StringWriter writer = new StringWriter(); velocityEngine.getTemplate(templateFullName).merge(context, writer); return writer.toString(); }
public void render(String template, Map<String, Object> map, OutputStream stream) { VelocityContext context = new VelocityContext(map); OutputStreamWriter writer = new OutputStreamWriter(stream); try { engine.mergeTemplate(baseTemplateDir + "/" + template, "UTF-8", context, writer); writer.flush(); } catch(Exception e) { throw new VoldemortException(e); } }
public String getContentAsString() { try { Template template = getTemplate(); StringWriter writer = new StringWriter(); template.merge(null, writer); return writer.toString(); } catch (Exception e) { throw new RuntimeException(e); } } }
public static FullMessage formatFullMessage(long userId, Event event, Position position) { VelocityContext velocityContext = prepareContext(userId, event, position); String formattedMessage = formatMessage(velocityContext, userId, event, position, "full"); return new FullMessage((String) velocityContext.get("subject"), formattedMessage); }
private void processUnion(Schema schema, GenerationContext genContext) { VelocityContext context = new VelocityContext(); context.put("schema", schema); context.put("generationContext", genContext); context.put("StyleUtils", StyleUtils.class); context.put("TypeConverter", TypeConverter.class); context.put("namespacePrefix", namespacePrefix); StringWriter headerWriter = new StringWriter(); engine.getTemplate("union.h.vm").merge(context, headerWriter); appendResult(headerWriter.toString(), true); StringWriter sourceWriter = new StringWriter(); engine.getTemplate("union.c.vm").merge(context, sourceWriter); appendResult(sourceWriter.toString(), false); }
/** * 融合模板和内容并写入到Writer * @param template 模板 * @param context 内容 * @param writer Writer */ public static void merge(Template template, VelocityContext context, Writer writer) { if(template == null) { throw new UtilException("Template is null"); } if(context == null) { context = new VelocityContext(globalContext); }else { //加入全局上下文 for (Entry<String, Object> entry : globalContext.entrySet()) { context.put(entry.getKey(), entry.getValue()); } } template.merge(context, writer); }
/** * 将Map转为VelocityContext * * @param bindingMap 参数绑定的Map * @return {@link VelocityContext} */ private VelocityContext toContext(Map<?, ?> bindingMap) { return new VelocityContext(bindingMap); }
private static String formatMessage(VelocityContext vc, Long userId, Event event, Position position, String templatePath) { VelocityContext velocityContext = vc; if (velocityContext == null) { velocityContext = prepareContext(userId, event, position); } StringWriter writer = new StringWriter(); getTemplate(event, templatePath).merge(velocityContext, writer); return writer.toString(); }
/** * 将Request中的数据转换为模板引擎<br> * 取值包括Session和Request * @param context 内容 * @param request 请求对象 * @return VelocityContext */ public static VelocityContext parseRequest(VelocityContext context, javax.servlet.http.HttpServletRequest request) { final Enumeration<String> attrs = request.getAttributeNames(); if (attrs != null) { String attrName = null; while (attrs.hasMoreElements()) { attrName = attrs.nextElement(); context.put(attrName, request.getAttribute(attrName)); } } return context; }
protected void processEnum(Schema schema, String template) { VelocityContext context = new VelocityContext(); List<String> symbols = schema.getEnumSymbols(); context.put("schema", schema); context.put("symbols", symbols); context.put("StyleUtils", StyleUtils.class); context.put("namespacePrefix", namespacePrefix); StringWriter writer = new StringWriter(); engine.getTemplate(template).merge(context, writer); appendResult(writer.toString(), true); }
@Override public void render(Map<?, ?> bindingMap, Writer writer) { rawTemplate.merge(toContext(bindingMap), writer); IoUtil.flush(writer); }
/** * 将Request中的数据转换为模板引擎<br> * 取值包括Session和Request * @param context 内容 * @param request 请求对象 * @return VelocityContext */ public static VelocityContext parseRequest(VelocityContext context, javax.servlet.http.HttpServletRequest request) { final Enumeration<String> attrs = request.getAttributeNames(); if (attrs != null) { String attrName = null; while (attrs.hasMoreElements()) { attrName = attrs.nextElement(); context.put(attrName, request.getAttribute(attrName)); } } return context; }
@Override public void render(Map<?, ?> bindingMap, Writer writer) { rawTemplate.merge(toContext(bindingMap), writer); IoUtil.flush(writer); }