How can I add some freemarker macro (<#macro myMacro>…) in java jar library and after than use it (<@macro myMacro/>) in my other web projects?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Step 1: embed a template file in your JAR library with a macro, e.g. a file
foo.ftlwith a macrobar.Step 2: configure FreeMarker so that it can load templates from that JAR library. One way to do this is registering a
ClassTemplateLoader, either by callingConfiguration.setClassForTemplateLoadingor by directly registering aClassTemplateLoader(see FreeMarker documentation about template loading). Alternatively you can try to use aURLTemplateLoaderor callConfiguration.setServletContextForTemplateLoading, depending on your use case.You maybe even have to combine such a
ClassTemplateLoader/URLTemplateLoaderwith your currently used template loader in order to load templates from more than one location (see MultiTemplateLoader).Step 3: import the macro template file into a namesapce from your main template via the import directive, e.g.
Step 4: call the macro via its namespace, e.g.
Done!