准备statics变量
BeansWrapper wrapper = new BeansWrapper(new Version(2,3,29);
TemplateModel statics = wrapper.getStaticModels();
调用process()之前把statics加到model里
public static boolean generate(String template, Map<String, Object> dataModel, Writer writer) {
dataModel.put("statics", statics);
//模板
Template temp = null;
try {
temp = cfg.getTemplate(template);
//输出
temp.process(dataModel, writer);
} catch (IOException | TemplateException e) {
e.printStackTrace();
return false;
}
return true;
}
使用statics加载静态类到变量里,直接用也可以
<#assign JSON=statics['com.alibaba.fastjson.JSON']>
使用
<#macro text content>
<#list content as frame>
<#if frame?size = 2>
<span style="
/*word-wrap:break-word;*/
<#assign arr1=JSON.parseArray(frame[1])>
<#list 0..arr1?size-1 as arr1Index>
<#assign arr2=arr1.getJSONArray(arr1Index)>
<#assign type=arr2.getString(0)>
<#switch type>
<#case "b">
font-weight:600;
<#break >
<#case "i">
font-style:italic;
<#break >
<#case "_">
border-bottom:0.05em solid;
<#break >
</#switch>
</#list>
">${frame[0]}</span>
<#else>
${frame[0]}
</#if>
</#list>
</#macro>
评论区