是否可以插入freemarker宏?

时间:2013-06-26 23:38:54

标签: template-engine freemarker templating string-interpolation templating-engine

说我有一个freemarker宏:

<#macro helloObject>
  World
</#macro>

我可以在某些情况下轻松地调用它:

Hello, <@helloObject/>!

但是插入它的语法是什么,例如,如果它是字符串的一部分?

<#assign greeting="Hello, <@helloObject>!"     /> <#-- doesn't work -->
<#assign greeting="Hello, ${helloObject}!"     /> <#-- doesn't work -->
<#assign greeting="Hello, ${@helloObject}!"    /> <#-- doesn't work -->
<#assign greeting="Hello, ${<@helloObject/>}!" /> <#-- doesn't work -->
<#assign greeting="Hello, @helloObject!"       /> <#-- doesn't work -->
${greeting}

2 个答案:

答案 0 :(得分:4)

您正在寻找function

<#function helloObject>
  <#return "World">
</#function>

${helloObject()} <#-- Will print out World -->    

<#assign greetings = "Hello, " + helloObject() />

${greetings} <#-- Will print out Hello, World --> 

无法分配宏的结果,因为它是模板片段。

答案 1 :(得分:4)

是的,你可以。

<#macro helloObject>
  World
</#macro>

<#assign x><@helloObject/></#assign>

Hello ${x}.

将打印:

Hello World.