Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call freemarker macro from different file

Tags:

freemarker

I have situation like this. I have folder a and inside it I have a.ftl and folder b. Inside folder b there is b.ftl. in b.ftl I have [#macro macroName param1]...[/#macro] How to call macro from file a? I have intellij idea warning. I saw this link https://intellij-support.jetbrains.com/hc/en-us/community/posts/206196939-Getting-Intellij-to-pick-up-Freemarker-macros so I tried to place

[#-- @implicitly included --]
[#-- @ftlroot "" --]

to the top of the a.ftl but it doesn't work. I was looking into freemarker docs http://freemarker.org/docs/ref_directive_macro.html but there is example just with calling macro from the same file.

like image 815
Spasoje Petronijević Avatar asked Jan 30 '23 09:01

Spasoje Petronijević


1 Answers

You could use <#import "/b/b.ftl" as b> (where the last b can be any variable name), and then you can call the macro like <@b.macroName ...>. Putting commonly needed macros/functions/values into a template that's #import-ed in other templates is common best practice.

You can also use <#include "/b/b.ftl"> in /a/a.ftl, and then you can just use <@macroName ...> (no namespace prefix like b.). The common use case of #include is however printing common output, as it executes the whole a.ftl, each time it's #include-d. If a.ftl only contains macro definitions and other such assignments that might not matter.

Imports and includes can also be executed automatically in the topmost template. For that, see the auto_imports (Configuration.setAutoImports) and auto_includes (Configuration.setAutoIncludes) configuration settings.

See also: http://freemarker.org/docs/dgui_misc_namespace.html

like image 131
ddekany Avatar answered Mar 15 '23 16:03

ddekany