Nested Java directives - best practice on how to make values available to inner directive?

"Alistair Israel" <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
I've searched the mailing list archives up to a several months back
but couldn't find anything on this.

What I'm basically trying to do is write a custom "@form" directive in
Java. Within the @form directive's body, I'd like to have various
"@input" directives (also written in Java). As to why I would want to
do this in Java rather than as a regular macro - I want the directives
to insert Javascript validation code based on annotations on the form
bean object.

So, conceptually, I'd like to be able to say:
<@form name="someForm" action="someAction">
<@input type="text" property="name"/>
</@form>

I'd like to make the value of @form name ("someForm") available to the
@input directive (so the @input directive can lookup the name from the
template model, inspect the class, find annotations and so on and so
forth.

At first I tried adding a local variable via
Environment.setLocalVariable() in my directive's execute(). This fails
with "Not executing macro body".

Delving through the code, it's apparent that while a custom macro
directive has currentMacroContext (that lets us define/access local
variables through the ".locals" special variable), a custom Java
directive doesn't. Why is that so?

So, I was able to make it work by using a global variable instead
(Environment.setGlobalVariable()). The only thing that bothers me is
that it really doesn't need to be a global variable - the scope of the
value of the current form name is just that: the current form. I could
conceptually overwrite or define a global variable that other
templates depend upon for other purposes.

[ Aside: perhaps if there was some way to use namespaces to
distinguish my directives (as well as any 'local' variables they need)
it wouldn't bother me as much. However, using
Configuration.setSharedVariable (via Spring's FreeMarkerConfigurer)
only lets me provide a simple string name - not sure if any prefixes
(e.g., "my.form") will be interpreted as a namespace. This, in fact,
was a separate question I meant to ask the list. ]

How would the members of the list suggest I do this - make a value
local to my parent directive available to nested directives - short of
explicitly passing them?

- Alistair A. Israel
-- 
http://alistairisrael.wordpress.com
http://jroller.com/alistair

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
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.