Re: Best way to pass parameter to custom TemplateLoader?

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
You should encode the relevant information from the HttpServletRequest
into the template name. For example, you could have template names
like "tree1://foo/bar.ftl" and "tree2://foo/bar.ftl" and so on. Then
your TemplateLoader should delegate to other TemplateLoader-s based on
the part before the "://" (which can be anything BTW, like even
"user=joe,application=accounting://"). If you wonder why
"tree1://foo/bar.ftl" is better than "tree1/foo/bar.ftl", it's because
absolute paths in bar.ftl, like in <#import "/baaz.ftl" as b>, will be
resolved to "tree1://baaz.ftl" in the first case, but to "baaz.ftl",
which is not correct. (You could still do cross-tree inclusions
explicitly, like with "tree2://something.ftl".)

-- 
Best regards,
 Daniel Dekany


Monday, July 11, 2011, 7:04:54 PM, 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.