有没有办法在 Apache Freemarker 中定义可选宏?

Is there way to define optional macros in Apache Freemarker?

使用宏进行继承的页面布局设计。我的主要网页布局是这样的

mainlayout.ftl

<div>
   <@head/>
   <@body/>
</div>

现在包含 mainlayout.ftl 的页面看起来有点像这样,可能会提供 head 宏

page1.ftl

<#include "/mainlayout.ftl"/>
<#macro body>
 ...........
</#macro>

由于 maylayout.ftl 需要 @head 宏,它会在页面呈现期间抛出错误。

有没有办法让@head宏可选?

在您的 mainlayout.ftl 中定义一个空头宏,您将必须在 page1.ftl 中定义它。

mainlayout.ftl

<#macro head></#macro>

<div>
   <@head/>
   <@body/>
</div>

page1.ftl

<#include "/mainlayout.ftl"/>
<#macro body>
 ...........
</#macro>