freemark #macro嵌套
warning:
这篇文章距离上次修改已过324天,其中的内容可能已经有所变动。
<#--
* 单文档标签
-->
<#macro document id="owner" custom="false" size="" condition="" clearHTML="false" cleanType="all" autoLink="false" target="_blank" startPos="0" dateFormat="yyyy-MM-dd" field="title" maxLength="0" truncatedFlag="...">
<@cms_content categoryId="${id}" islist="false" size="${size}" startPos="${startPos}">
<#list DataList as document>
<#if custom="true">
<#global document = document />
<#nested>
<#else>
<#if (field="createTime" || field="updateTime" || field="publishDate" || field="offlineDate" || field="topDate")>
<#assign data = date_format(document["${field}"],"${dateFormat}") />
<#local htmlContent = data>
<#else>
<#local htmlContent = document["${field}"]>
</#if>
<#if clearHTML="true">
<#local htmlContent = dealArticleBody(htmlContent,"${cleanType}")>
</#if>
<#if autoLink="true" >
<a href="${contentUrl(document.id, 'pc')}" target="${target}">
<#if (maxLength?number gt 0 && htmlContent?length gt maxLength?number)>
${htmlContent?substring(0,maxLength?number)}${truncatedFlag}
<#else>
${htmlContent}
</#if>
</a>
<#else>
<#if (maxLength?number gt 0 && htmlContent?length gt maxLength?number)>
${htmlContent?substring(0,maxLength?number)}${truncatedFlag}
<#else>
${htmlContent}
</#if>
</#if>
</#if>
</#list>
</@cms_content>
<@documentStatVisit/>
</#macro>
<#--
* 文章浏览量统计
-->
<#macro documentStatVisit>
<script type="text/javascript">
(function () {
var q = [];
q.push('sid=${Site.id}');
q.push('cid=${Category.id}');
q.push('id=${Document.id}');
var s = document.createElement("script");
s.src = '${ApiPrefix}api/stat/visit?' + q.join('&');
(document.getElementsByTagName("head")[0] || document.getElementsByTagName("body")[0]).appendChild(s);
})();
</script>
<!--<@cms_document_stat_visit></@cms_document_stat_visit>-->
</#macro>
使用 <@documentStatVisit/>
freemarker.template.Configuration cfg = new freemarker.template.Configuration(
freemarker.template.Configuration.VERSION_2_3_31);
cfg.setDefaultEncoding(properties.getDefaultEncoding());
// 模板加载路径
MultiTemplateLoader multiTemplateLoader = new MultiTemplateLoader(
fileTemplateLoaders.stream().toArray(FileTemplateLoader[]::new));
cfg.setTemplateLoader(multiTemplateLoader);
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
cfg.addAutoImport("jw","/jtag.html");