Re: Import macros into global namespace
Chris <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
> There is setAutoImports and then there is setAutoIncludes. What I'm
> asking is if you have tried setAutoIncludes. That does the same as
> #include (as opposed to #import).
Excellent! Works perfectly. Keeps things much cleaner.
> So this is only interesting if you want make all the stuff uniformly
> accessible with that "mynamespace" perfix (as opposed to making them
> uniformly accessible without any prefix - for that see the #include
> trick earlier). Regarding how to achieve this, now that I think more
> about it, I think the cleanest solution is this:
>
> MySpecialTemplateHashModel mynamespace = new MySpecialTemplateHashModel(...);
> root.put("mynamespace", mynamespace)
> Environment env = myTemplate.createProcessingEnvironment(root, out);
> mynamespace.setDefaults(env.importLib("/include/mymacros.ftl", "iDontCare"))
> env.process(); // and only then process the template
>
This is slick, and might come in handy later. I didn't realize that
env.importLib() returned a usable hash. You could use it to do all kinds
of things programmatically. Like enabling / disabling access to
different libraries or macros based on, well, anything. Or transparently
merging multiple related libraries under one namespace.
------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb