sme*_*eeb 8 layout freemarker templating
根据FreeMarker include语句文档,您可以像这样制作页眉和页脚感知模板:
<#include "/header.ftl">
<!-- Content of my this page -->
<#include "/footer.ftl">
Run Code Online (Sandbox Code Playgroud)
但如果我的网络应用程序有数百个页面/视图,这是一个冗余的复制意大利面.如果在FreeMarker中有一个" 布局 "概念,那就太棒了,我可以说" 嘿,这是一个布局: "
<#include "/header.ftl">
<@import_FTL_Somehow>
<#include "/footer.ftl">
Run Code Online (Sandbox Code Playgroud)
然后为每个视图创建/页(单独的模板index.ftl,contactUs.ftl等等),然后告诉FreeMarkers其中FTL文件"使用"的布局.这样我就不必在每个模板文件中继续指定页眉/页脚包含.
FreeMarker是否支持这种概念?
它没有,但是如果你只需要一个页脚或标题,它可以通过一些TemplateLoader黑客来解决(TemplateLoader插入页眉和页脚片段,就好像在模板文件中一样).但FreeMarker中的常用解决方案是从每个模板中明确调用布局代码,但不是#include直接使用两个-s,而是像:
<@my.page>
<!-- Content of my this page -->
</@my.page>
Run Code Online (Sandbox Code Playgroud)
哪里my是自动导入(请参阅参考资料Configuration.addAutoImport).
更新:另一种方法是你有一个layout.ftl像:
Heading stuff here...
<#include bodyTemplate>
Footer stuff here...
Run Code Online (Sandbox Code Playgroud)
然后从Java总是调用layout.ftl,但也使用bodyTemplate变量传递正文模板名称:
dataModel.put("bodyTemplate", "/foo.ftl");
cfg.getTemplate("layout.ftl").process(dataModel, out);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3004 次 |
| 最近记录: |