Re: Import macros into global namespace

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Monday, February 7, 2011, 9:40:49 PM, Chris wrote:

> Is it possible to import a library of macros into the main namespace? 
> We'd like to do this using Configuration.autoImport().

Doesn't Configuration.setAutoIncludes do what you want?

> Here's the issue. For our app, we have a library of macros and a bunch
> of variables in the datamodel. We also have a few directives in the 
> datamodel. We're currently forced to do this:
>
> ${myvariable}
> [@mynamespace.mymacro/]
> [@mydirective/]
>
> It's gotten confusing and frustrating for our designers because 
> sometimes they have to use the namespace and sometimes not.
>
> It doesn't seem possible to put the datamodel into the same namespace as
> the library. If I do this:
>
> HashMap rootMap = new HashMap();
> HashMap myVariables = new HashMap();
> myVariables.put("foo", "bar");
> rootMap.put("mynamespace", myVariables);
>
> then ${mynamespace.foo} yields a "not found" error. If I get rid of the
> library autoimport then it works ok.

Yeah, the two hashes shadow each other, and the one created by #import
has the priority. Maybe this can be worked around if you import the
macro library with a non-conflicting name, then pass the macro library
(the hash that #import creates) back to the data-model through a
custom method of the a custom data-model root class, which then uses
that for fall-backs. I never tried to do this, so I'm not sure if it
has a catch...

Also note that Directives can be part of your FTL macro library using
<#assign foo = "com.example.FooDirective"?new()>.

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
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
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.