API / Apache FreeMarker Interview questions
What is the difference between #assign and #local?
#local can only be used inside a macro or function body, and it creates a variable that is private to that single call - it disappears once the macro returns and never leaks into the calling template's namespace.
<#macro greet name> <#local upperName = name?upper_case> Hello, ${upperName}! </#macro>
#assign used inside that same macro, without #local, does not create a private variable - it still targets the enclosing namespace, which means it can accidentally overwrite a variable of the same name outside the macro. This is a common source of bugs: forgetting #local inside a macro when a truly private helper variable was intended.
More Related questions...