Rick
Rick

Reputation: 1607

Creating a freemarker macro with optional nested content?

What is the proper way to create a macro that can be used with or without nested content? e.g.

<@myMacro/>
<@myMacro>my nested content</@myMacro>

Is there something like this? Or something else?

<#macro myMacro>
  <#if ??????>
    Before nested content
    <#nested/>
    After nested content
  <#else/>
    Nothing nested here
  </#if>
</#macro>

Upvotes: 3

Views: 1145

Answers (1)

Goose
Goose

Reputation: 831

The nested content is assigned to a variable and then this variable is checked if it has any content. The variable is then sent to the output instead of using another nested directive to avoid the content being processed twice.

<#macro myMacro>
    <#assign nested><#nested/></#assign>

    <#if nested?has_content>
        Before nested content
        ${nested}
        After nested content
    <#else/>
       Nothing nested here
    </#if>
</#macro>

Upvotes: 6

Related Questions