API / Apache FreeMarker Interview questions
What is the difference between a macro and a custom directive (TemplateDirectiveModel)?
Both let a template author write <@name ...>, but they are implemented very differently and suit different jobs.
| Aspect | Macro (#macro) | Custom directive (TemplateDirectiveModel) |
| Implemented in | FTL, inside a template | Java, compiled into the application |
| Access to raw Environment | No | Yes - full control over output and parsing |
| Typical author | Template/page authors | Application developers |
| Good for | Reusable page fragments, layout snippets | Cross-cutting utilities: security checks, formatting helpers, i18n lookups |
A rule of thumb: reach for a macro when the reusable piece is really just more FTL markup, and reach for a custom directive when the behavior needs Java-level logic, access to the environment, or must be shared as a packaged library across many unrelated template sets.
More Related questions...