PDA

View Full Version : Can I load Freemarker templates from database?


marvi
Oct 7th, 2005, 03:48 PM
If I run Freemarker standalone I can implement my own TemplateLoader. In Spring MVC it seems I will have to use SpringTemplateLoader and it can use either classpath: or file: to find the templates.

If I would like to chain two template loaders, one that looks in a database and one looking in the classpath, how would I implement that?

davison
Oct 7th, 2005, 08:39 PM
you'll need to subclass FreeMarkerConfigurer, overriding postProcessConfiguration() with something similar to below:
protected void postProcessConfiguration(Configuration config) {
super.postProcessConfiguration(config);
TemplateLoader loader = config.getTemplateLoader();
MyDbLoader dbLoader = new MyDbLoader();
TemplateLoader[] loaders = new TemplateLoader[] {loader, dbLoader};
MultiTemplateLoader multiLoader = new MultiTemplateLoader(loaders);
config.setTemplateLoader(multiLoader);
}
which basically replicates what FreeMarkerConfigurer itself does to add the Spring macro loader.

This could be better - we should probably have a setTemplateLoaders(TemplateLoader[]) on FreeMarkerConfigurationFactory.

If you want to raise a JIRA issue for this I'll take a look at it. May be possible to do similar for Velocity, although it can be specified in property file config there.

Regards,

marvi
Oct 8th, 2005, 04:28 AM
Thanks!

SPR-1360

marvi
Oct 9th, 2005, 05:31 PM
I can't get your solution to work. When debugging it seems like the TemplateLoader I get from getTemplateLoader() is already a MultiTemplateLoader, consisting of one FileTemplateLoader and one ClassTemplateLoader. So when I add my own StringTemplateLoader I get one MultipleTL containing one MultipleTL and one StringTL.

Freemarker finds the templates in MultiTL->MultiTL->FileTL but not the ones in MultiTL->StringTL.

davison
Oct 9th, 2005, 06:12 PM
it seems a bit odd that the FM guys decided on this MultiTemplateLoader implementation anyway instead of just maintaining a Collection of TemplateLoaders that you could add to as needed. You might want to check their docs or post to their lists to see what the thinking there was.

If it doesn't work with the multiple nested MTL's, then you'll probably need to wait until I can resolve the JIRA issue.

I'll take a look at it shortly and update JIRA.

Regards,

Daniel Dekany
Oct 10th, 2005, 07:32 PM
You can nest MultiTemplateLoader-s into each other, that should work. I have even tried it (not with Spring, but that certainly doesn't mater).

davison
Oct 11th, 2005, 03:49 AM
thanks Daniel,

It seems from the OP that this worked on one of the nested MTL's but not the other..

Are you able to give any insight as to why MultiTemplateLoader was chosen as opposed to a Collection of TemplateLoaders? Just curious :)

Regards,

Daniel Dekany
Oct 11th, 2005, 07:20 AM
I wasn't there when that decision was made, so I don't know... But I think that it's bascially just matter of taste. The featuritis approach (where you want to address all imaginable usecase right in the core) is collectinos, the minimalist approach is pushing the task to the TemplateLoaderImplementation thus keeping the core API simpler. I might be rather on the side of collection stuff like you, but no big difference IMO.

davison
Oct 12th, 2005, 08:11 PM
Thanks!

SPR-1360

resolved.. http://opensource2.atlassian.com/projects/spring/browse/SPR-1360