我的一些FreeMarker宏具有接受其他宏的参数。这允许我拥有页面的标准部分,可以在奇怪的场合进行更改。在下面的示例中,文章宏默认使用defaultHeader。有时我通过传递alternativeHeader宏作为header参数来使用alternativeHeader。这很好。
有时我想传递alternativeHeader宏但更改其text参数。我知道如何做到这一点的唯一方法是创建另一个“调用者”宏,然后使用它。但是,这意味着我必须将这些“调用者”宏添加到很多页面中,它们看起来很混乱。还有另一种方法可以直接传递alternativeHeader宏,同时还设置其参数吗?
<html>
<head><title>test</title></head>
<body>
<@article body="First example"/>
<@article body="Second example" header=alternativeHeader/>
<@article body="Third example" header=alternativeHeaderCaller/>
</body>
</html>
<#--
These three macros would usually be contained in a library.
-->
<#macro article body header=defaultHeader>
<div>
<@header/>
<br>
${body}
</div>
</#macro>
<#macro defaultHeader>
This is a default header
</#macro>
<#macro alternativeHeader text="This is an alternative header">
${text}
</#macro>
<#--
I want to avoid having to add and use this macro.
-->
<#macro alternativeHeaderCaller>
<@alternativeHeader text="I have changed this header"/>
</#macro>
输出
<html>
<head><title>test</title></head>
<body>
<div>
This is a default header
<br>
First example
</div>
<div>
This is an alternative header
<br>
Second example
</div>
<div>
I have changed this header
<br>
Third example
</div>
</body>
</html>
我正在寻找类似的东西
<@article body="Third example" header=alternativeHeaderCaller(text="My other text")/>
答案 0 :(得分:1)
我担心你不能做得更好...... FTL中没有“currying”功能。