min-velocity是一个专为代码生成而定制的简化velocity模板引擎。
目标:以velocity1.7为基础,裁剪出适合用作代码生成的模板引擎
裁剪:没有event机制
没有macro
没有stop
没有evaluate
没有define
没有break
改动:requiresjdk1.5+
默认情况下,不打印任何日志
默认采用classpath模板加载器而非文件系统模板加载器
defaultI/OencodingchangedtoUTF-8(fromiso-8859-1)
对于#set指令,默认允许设置null值
默认打开resourcecache
去掉了parserpool
#parse和#include标签支持相对路径
新增$ParseUtil.recParsing("xxx.vm").addParam("key",val)模板调用形式;相当于带调用栈的#parse标签,能用在当你需要每层递归的context都相互隔离的递归#parse的时候;也能支持相对路径
可放置min-velocity.properties文件(可选)在classpath根路径下,用于覆盖velocity的各种默认属性
min-velocity.properties可使用default.static.util.mappings属性配置默认的静态工具类,这些工具类将被默认放入模板context中,可配置多个,如:default.static.util.mappings=ClassUtils:org.apache.commons.lang.ClassUtils
设置'stream.reference.rendering'开关(true/false),默认关闭;开启后,遇到reference是stream或reader的时候,将读取stream或reader中的内容做渲染而非简单地toString渲染;其中读取stream或reader的buffer可通过'stream.reference.rendering.buffer.size'配置大小(默认为1024个字符);亦可通过'stream.reference.rendering.limit'选项设置能够从流中读取的最大字符数限制(默认为100000)
支持String模板渲染,即直接将模板内容以String形式传入api进行渲染而不是只能选择传入一个模板路径
新增index.out.of.bounds.exception.suppress选项,当设置为true时,模板中对数组或list进行的取值或设置操作将忽略indexoutofbounds异常
ForEnglishspeakers,seebelow:Noeventmechanism
Nomacro
No'#stop'
No'#evaluate'
No'#define'
No'#break'
requiresjdk1.5+
Bydefaultnologsratherthanlogtovelocity.log
defaultstouseclassapthresourceloader
I/OencodingdefaultstoUTF-8
#setdirectivedefaultstoallownullvalue
resourcecacheonbydefault
parserpoolremoved
relativepathsupportfor#parseand#includedirectives
$ParseUtil.recParsing("xxx.vm").addParam("key",val)templateparsingutiladded.Youcanseeitasa'#parse'directivewithinvocationstackframe,whichcouldeasilydorecursiveparsingwithisolatedcontextineachroundofrecursion.Thisalsosupportsrelativepath.
couldplaceanoptional'min-velocity.properties'fileinclasspathroottoconfigurevelocityruntime.
min-velocitycouldcontainzeroormore'default.static.util.mappings'propertyconfigstoexposestaticutilityclassesintemplatecontexts,forexample:default.static.util.mappings=ClassUtils:org.apache.commons.lang.ClassUtils,withthisconfigyoucanreferencetoorg.apache.commons.lang.ClassUtilsclasswithkey'ClassUtils'anywhere.
stream/readerreferencerenderingsupported.Ifyouset'stream.reference.rendering'(defaultfalse)to'true',min-velocitywilldumpthecontentsofastream/readerreferenceratherthanjustinvoking'toString'onthemwhilerendering.Andthestream/readerreadingbuffersizecouldbespecifiedbyconfiguration'stream.reference.rendering.buffer.size',measuredinnumberofcharacters(default1024).Andfurthermore,themaximumnumberofcharactersreadfromastreamcouldbelimitedbyconfiguration'stream.reference.rendering.limit'(default100000).
Stringliteraltemplatesrenderingsupported.Justspecifytemplatecontentsinain-memory-Stringvaluetorender,otherthanalwaysspecifyatemplatepath.
When'index.out.of.bounds.exception.suppress'optionissettingtobe'true',any'IndexOutOfBoundsException'willbeignoredwhenaccessingorsettingelementsofarraysandlists.
MavenCentralRepo:<dependency> <groupId>com.github.pfmiles</groupId> <artifactId>min-velocity</artifactId> <version>1.0</version></dependency>代码样例参见单元测试:
package com.github.pfmiles.minvelocity;import java.io.StringReader;import java.io.StringWriter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import junit.framework.TestCase;import com.github.pfmiles.org.apache.velocity.Template;public class TemplateUtilTest extends TestCase { public void testRenderStringTemp() { String templateString = "#foreach($i in $list)\n$i\n#end"; Map<String, Object> ctxPojo = new HashMap<String, Object>(); List<String> list = new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); ctxPojo.put("list", list); StringWriter out = new StringWriter(); TemplateUtil.renderString(templateString, ctxPojo, out); // System.out.println(out.toString()); assertTrue("one\ntwo\nthree\n".equals(out.toString())); } public void testRenderTemplate() { Template temp = TemplateUtil.parseStringTemplate("#foreach($i in $list)\n$i\n#end"); Map<String, Object> ctxPojo = new HashMap<String, Object>(); List<String> list = new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); ctxPojo.put("list", list); StringWriter out = new StringWriter(); TemplateUtil.renderTemplate(temp, ctxPojo, out); // System.out.println(out.toString()); assertTrue("one\ntwo\nthree\n".equals(out.toString())); } public void testRefRendering() { Template temp = TemplateUtil.parseStringTemplate("hello $ref world"); Map<String, Object> ctxPojo = new HashMap<String, Object>(); StringReader stream = new StringReader("1234567890"); ctxPojo.put("ref", stream); StringWriter writer = new StringWriter(); TemplateUtil.renderTemplate(temp, ctxPojo, writer); assertTrue("hello 1234567890 world".equals(writer.toString())); }}
评论