API / Apache FreeMarker Interview questions
How do you create a user-defined macro using #macro?
<#macro name param1 param2=default> ... </#macro> defines a reusable block of template markup, written entirely in FTL, that can accept parameters, including optional ones with default values.
<#macro button label href target="_self"> <a href="${href}" target="${target}" class="btn">${label}</a> </#macro> <@button label="Docs" href="/docs"/>
A macro can also wrap arbitrary nested content and render it back out with the <#nested> directive, which is how a macro can act like a layout container rather than just a parameterized snippet:
<#macro card> <div class="card"><#nested></div> </#macro> <@card>This content is nested inside the card.</@card>
More Related questions...