开发者

Add freemarker macro in jar library

How can I add some freemarker macro (<#macro myMac开发者_如何学Pythonro>...) in java jar library and after than use it (<@macro myMacro/>) in my other web projects?


Step 1: embed a template file in your JAR library with a macro, e.g. a file foo.ftl with a macro bar.

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 calling Configuration.setClassForTemplateLoading or by directly registering a ClassTemplateLoader (see FreeMarker documentation about template loading). Alternatively you can try to use a URLTemplateLoader or call Configuration.setServletContextForTemplateLoading, depending on your use case.

You maybe even have to combine such a ClassTemplateLoader/URLTemplateLoader with 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.

<#import "foo.ftl" as foo>

Step 4: call the macro via its namespace, e.g.

<@foo.bar />

Done!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜