'Using external css in freemarker when string temlpate loader is used

Case 1 : I have used <#include <cssFilePath>/style.css>in ftl when template file is loaded from template directiory(setClassForTemplateLoading i was using in this case) as file and was able to do all styling work.

Case 2: Now, As my ftl and css files will be moved to db.I am planing to use StringTemplateLoader for loading freemarker template as string. But how can i use the css file which will be loaded from db as string in the ftl templates?



Solution 1:[1]

If some templates will come from a StringTemplateLoader, and some others (the CSS) from some other TemplateLoader (the one that loads from DB), then you can use MultiTemplateLoader, which searches templates in multiple TemplateLoaders.

Also note that you can create Template-s directly from a String with the Template(String name, String sourceCode, Configuration cfg) constructor. Then #include will still use the TemplateLoader set in the FreeMarker Configuration. Caching the Template-s (if you need that for performance reasons) that you created directly from a String is up to you though.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ddekany