FreeMarker 自定义指令(三)

1. 模板文件 test04.ftlfoo<@customUpper>bar f<#– 这里允许使用所有的 FTL –><#list [“red”, “green”, “blue”] as color>${color}</#list>baaz</@customUpper>wombat2. 自定义指令类 UpperDirective.javaps: 一个把字符串变为大写的指令package com.freemarker.test04.Directives;import java.io.IOException;import java.io.Writer;import java.util.Map;import freemarker.core.Environment;import freemarker.template.TemplateDirectiveBody;import freemarker.template.TemplateDirectiveModel;import freemarker.template.TemplateException;import freemarker.template.TemplateModel;import freemarker.template.TemplateModelException;/*** FreeMarker 的用户自定义指令在逐步改变* 它嵌套内容的输出转换为大写形式* <p><b>指令内容</b></p>* <p>指令参数:无* <p>循环变量:无* <p>指令嵌套内容:是*/{(Environment env,Map params, TemplateModel[] loopVars,TemplateDirectiveBody body)throws TemplateException, IOException {// 检查参数是否传入if (!params.isEmpty()) {throw new TemplateModelException(“This directive doesn’t allow parameters.”);}if (loopVars.length != 0) {throw new TemplateModelException(“This directive doesn’t allow loop variables.”);}// 是否有非空的嵌入内容if (body != null) {// 执行嵌入体部分,和 FTL 中的<#nested>一样,除了// 我们使用我们自己的 writer 来代替当前的 output writer.body.render(new UpperCaseFilterWriter(env.getOut()));} else {throw new RuntimeException(“missing body”);}}/*** {@link Writer}改变字符流到大写形式,* 而且把它发送到另外一个{@link Writer}中。*/{private final Writer out;UpperCaseFilterWriter (Writer out) {this.out = out;}(char[] cbuf, int off, int len)throws IOException {char[] transformedCbuf = new char[len];for (int i = 0; i < len; i++) {transformedCbuf[i] = Character.toUpperCase(cbuf[i + off]);}out.write(transformedCbuf);}() throws IOException {out.flush();}() throws IOException {out.close();}}}3. 测试类文件 Test.javapackage com.freemarker.test04.Directives;import freemarker.template.*;import java.util.*;import java.io.*;public class Test {(String[] args) throws Exception {// 创建 Freemarker 配置实例Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);// 指定模板文件从何处加载的数据源,这里设置成一个文件目录。cfg.setDirectoryForTemplateLoading(new File(“templates”));cfg.setDefaultEncoding(“UTF-8”);// 简单地重新抛出异常; 这应该在大多数生产系统中使用。cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);// 创建一个数据模型Map root = new HashMap();// 测试自定义指令 ——————————–root.put(“customUpper”, new UpperDirective());// 获取模板(使用内部缓存)Template temp = cfg.getTemplate(“test04.ftl”);// 合并数据模型模板Writer out = new OutputStreamWriter(System.out);temp.process(root, out);out.flush();out.close();// 注意: ————// 为了简单起见,这里压制了异常(在方法签名中声明了异常,译者注),而在正式运行的产品中不要这样做。}}运行结果fooBAR FREDGREENBLUEBAAZwombat

,我要扼住命运的咽喉。

FreeMarker 自定义指令(三)

相关文章:

你感兴趣的文章:

标签云: