Re: Another Idea/Question

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Saturday, November 8, 2008, 6:29:38 PM, Luther Baker wrote:

> Hope I'm not bothering anyone's time but wanted to run another idea past you.
>
> I know that a macro can be implemented by a Java class as well as a text template.
>
> I have a desire to do 'both' simultaneously.
>
> In otherwords, I would like write
>
> <@editor user/> (where 'user' is some simple POJO that exists on
> the page with certain properties).
>
> and have it invoke a Java class (to populate the Map) and then a template (which uses the Map)
>
> For example:
>
> a) Invoke x.y.z.Editor.execute(Map mapOut, Args args) 
>
> { 
> /*reflect into arg[0] and determine props or just hardcode as here */ 
> mapOut.put("fieldOne", user.firstName); mapOut.put("fieldTwo", user.lastName);
> }
>
> b) Invoke like named macro
>
> <#macro editor>
> ${fieldOne} ${fieldTwo}
> </#macro>
>
> This is obviously a simple example. I think I can do this
> indirectly by creating a general Java macro that would perform steps A and B like this:
>
> <@DualMacro editor/>
>
> Where x.y.z.DualMacro.execute(...) could then instantiate the class
> (Editor.class) and load the like-named template. I would end up with a bunch of
>
> <@DualMacro table/>
> <@DualMacro signupForm/>
>
> but it would still work. Is there a more elegant way to do this in FM?

I think the usual way of doing this is that you write the "editor"
macro in FTL, and then that macro calls out for the java code
explicitly:

<#macro editor>
  <#local m = macroHelpers.editor()> <#-- calls a Java method -->
  ${m.fieldOne} ${m.fieldTwo}
</#macro>

(In any case, conveniently calling a macro has much higher priority
than conveniently defining it, so writing <@DualMacro table/> and like
into your templates is most certainly a bad idea.)

-- 
Best regards,
 Daniel Dekany


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
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.