package com.ninemax.test;
import java.io.File;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class FreeMarkerByTwo {
@SuppressWarnings("all")
public static void main(String[] args) throws Exception {
// 创建 freemarker配置实例
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("D:\\freemarker\\src"));
// 创建数据类型
Map<String, Object> map = new HashMap<String, Object>();
map.put("type", "other");
map.put("num1", 2);
map.put("num2", 3);
// 加载模板文件
Template t = cfg.getTemplate("macro.ftl");
//显示生成后的数据
t.process(map, new OutputStreamWriter(System.out));
}
}
marco.ftl文件:
----------------------宏指令的使用 m1可以看成是方法的名称,num1、num2为入参----------------------
<#macro m1 num1 num2>
<#assign result=num1+num2>
<h3>${result}</h3>
</#macro>
<@m1 5 6/>
----------------------宏指令(嵌入式)-----------------
<#macro m2>
<h3><#nested></h3>
</#macro>
<@m2>hello world</@m2>
输出:
----------------------宏指令的使用 m1可以看成是方法的名称,num1、num2为入参----------------------
<h3>11</h3>
----------------------宏指令(嵌入式)-----------------
<h3>hello world</h3>