Re: Custom Loader Follow-up
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Wednesday, May 27, 2009, 4:33:56 PM, Scott Purcell wrote:
> I am spending way too much time on this Freemarker configuration
> for a stand-alone application. I am not dealing with a web-app.
(Well, in your own Web app. framework you wouldn't; it's trivial to
achieve what you want if you have "direct" access to FreeMarker. Of
course, if FreeMarker were more IoC-friendly, configuring it would be
certainly easier in Spring too...)
> I need to be able to use my DB for templates. So I created two
> custom TemplateLoaders. I added the following in my Spring.xml file.
> <bean id="freemarkerLoaderConfiguration"
> class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactory">
> <property name=templateLoaders>
> <list>
> <ref bean="eventTemplateLoader" />
> <ref bean="EventTemplateLoaderOther" />
> </list>
> </property>
> </bean>
>
> Each template serves a specific template. Or at least that is how we want it to work.
That's kind of odd, but possible. A TemplateLoader usually corresponds
to a *collection* of templates (like to a table in a DB, where the
each record stores a template name and a template content).
> Now I have a test harness to pull the templates out and run the
> conversion on them. This is where all the problems begin to fold.
> I have the folloiwng in a test class.
>
> // first pull out the bean
> FreeMarkerConfigurationFactory factory = (
> FreeMarkerConfigurationFactory
> )SpringUtil.getApplicationContext().getBean("freemarkerLoaderConfiguration");
> Configuration config = factory.createConfiguration();
> // how to pull by name, as a list doesn't have names.
> Template template =
> config.getTemplate("EventTemplateLoaderOther");
>
> Not this works, but it always pulls the first
> "eventTemplateLoader" from the list out. There is no where to
> associate a name with the class using this mechanism. So how is this accomplished?
I know nothing about Spring's "templateLoaders" feature, but you seem
to be confused about FreeMarker TemplateLoaders itself. In
config.getTemplate("foo"), the "foo" is the name of the *template*;
TemplateLoaders themselves has no name as far FreeMarker is concerned.
Also, the Configuration has only one TemplateLoader (that of course
possibly internally uses multiple TemplateLoaders). Spring certainly
packs all those listed TemplateLoaders into a single one (maybe using
freemarker.cache.MultiTemplateLoader), and set that united
TemplateLoader object as the TemplateLoader of the Configuration
object. So most probably, when you call config.getTemplate("foo"), the
"foo" template is searched by the TemplateLoader object that you have
given id "eventTemplateLoader", and only if that template loader
didn't find a template called "foo", only then it is searched by
TemplateLoader that you have given id "EventTemplateLoaderOther", etc.
That's how it used to work.
> Thanks,
>
>
>
> CONFIDENTIALITY NOTICE
> This e-mail message and any attachments are only for the use of the
> intended recipient and may contain information that is privileged,
> confidential or exempt from disclosure under applicable law. If you
> are not the intended recipient, any disclosure, distribution or
> other use of this e-mail message or attachments is prohibited. If
> you have received this e-mail message in error, please delete and
> notify the sender immediately. Thank you.
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, &
iPhoneDevCamp as they present alongside digital heavyweights like Barbarian
Group, R/GA, & Big Spaceship. http://p.sf.net/sfu/creativitycat-com