Reputation: 403
how can I have few different #nested elements in macros?
Upvotes: 10
Views: 6435
Reputation: 31
The #nested
directive can have variables. So when calling macro we can check it's value.
<#macro drawHtmlBlock>
<div class="html_block">
<div class="html_block__header">
<#nested 'header'/>
</div>
<div class="html_block__footer">
<#nested 'footer'/>
</div>
</div>
</#macro>
<@drawHtmlBlock ; contentType>
<#if contentType == 'header'>
header_content
<#elseif contentType == 'footer'>
footer_content
</#if>
</@drawHtmlBlock>
And we get the result:
<div class="html_block">
<div class="html_block__header">
header_content
</div>
<div class="html_block__footer">
footer_content
</div>
</div>
Upvotes: 1
Reputation: 419
You cant have different #nested elements in a macro, every usage of it will output the same text.
If you goal it to have multiple variable sections in your macro, you can use the #assign element.
Example of a page #macro allowing to define the body, header and footer content :
<#macro pageTemplate header="" footer="">
${header}
<#nested >
${footer}
</#macro>
You can then define each section using the #assign element (but admittedly having multiple named #nested element would be better).
<#assign headerContent>
This is the header.
</#assign>
<#assign footerContent>
This is the footer.
</#assign>
<@pageTemplate header=headerContent footer=footerContent>
This is the nested content.
</@pageTemplate>
The resulting output will be :
This is the header.
This is the nested content.
This is the footer.
Upvotes: 30