Re: Best way to pass parameter to custom TemplateLoader?

Attila Szegedi <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Hm… this won't work as you'll unfortunately run into the issue where templates are already cached. If FreeMarker loads a template for a given path, it will be cached and not reloaded next time, but a cached parsed representation will be used instead. TemplateLoader is not even consulted in this case. This is by design as parsing is expensive.

In general, the idea of having the same path resolving to different templates within the same Configuration object based on some external parameters (i.e. request data) is bad. 

In this case, I think you should keep around several different Configuration instances, each with differently configured template loader, and select the one to use based on your HttpServletRequest data (you can even have the Configuration to use passed around as a request attribute, set by a filter that did the initial inspection of data).

Yet another idea is to have a map from your canonical template paths to your translated template pats, and use those to figure out the actual path for Configuration.getTemplate(), as well as for and [#include ] directive -- include directives don't just take string literals as their parameters, they can take expressions, so you can put your map into the data model, i.e. under name "translatedPathsMap", and then do [#include translatedPathsMap("/my/untranslated/path.ftl")]. Of course, you don't get the benefit of relative path resolution, acquisition, etc. with that approach…

Attila.
 
On Jul 11, 2011, at 10:04 AM, Chris wrote:

> I'm trying to implement a custom TemplateLoader for freemarker. This 
> loader needs to examine the HttpServletRequest to know where to get the 
> template source.
> 
> Unfortunately, Configuration.getTemplate() does not provide a way to 
> pass arbitrary parameters to the TemplateLoader.
> 
> One possible solution is to use some kind of ThreadLocal scheme. I'm 
> concerned that this may not work because in Jetty, our app server, 
> there's some fancy nio module that handles multiple connections per 
> thread, and I don't entirely understand how it works. Plus I'm 
> suspicious of anything that could lead to hard-to-debug threading issues.
> 
> Another way would be to create an Environment variable and add a custom 
> attribute to it, but unfortunately it appears that an Enviroment can 
> only get created after the template has been created.
> 
> I'm stuck.
> 
> 
> ------------------------------------------------------------------------------
> All of the data generated in your IT infrastructure is seriously valuable.
> Why? It contains a definitive record of application performance, security 
> threats, fraudulent activity, and more. Splunk takes this data and makes 
> sense of it. IT sense. And common sense.
> http://p.sf.net/sfu/splunk-d2d-c2
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security 
threats, fraudulent activity, and more. Splunk takes this data and makes 
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.