Re: processing a macro from a Freemarker user-defined directive

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Wednesday, November 10, 2010, 6:44:18 PM, Rebecca Younes wrote:

> Hello,
>
> I am trying to write a Freemarker directive in Java that will
> process a macro in a template, rather than the entire template. Is
> there a way to do this? I can see how to get the macro using
> Template.getMacros(), but I do not see a method of Macro that would
> allow me to process that.

Not like I ever tried to do this, but I'm pretty certain that no
public API-s exist to call a macro from Java. It's kind of finny,
because you can easily call other macros when you implement a macro in
FTL, yet you can't do that when you implement similar stuff in Java
(i.e. a TemplateDirectiveModel), as far as I see. Anyway, it's all
what you need, a plain equivalent of <@someMacro /> but from Java?

(I haven't done much programming in FreeMarker, but I think I could
implement that, calling macros from Java, and I also have the
authority do such things. Unfortunately there is a good chance that I
wont find much time in the foreseeable future... and Jonathan and
Attila seems to be out. So I just can't promise anything.)

> The Freemarker/Velocity comparison at
> http://freemarker.sourceforge.net/fmVsVel.html states:
>  You can capture the output of an arbitrary part of the template into a context variable.

That "arbitrary" has to be enclosed between explicit tags. Like

  <#assign blah>
    ... the arbitrary part ...
  </#assign>

This captures the output printed between the two tags into the "blah"
variable. Similarly, a TemplateDirectiveModel can capture the output
generated between that calling start- and end-tags, into whatever it
wants to. I have seen some (like Nuxeo developers) used this for
defining named "fragments" or "multiple bodies". Kind of a hack IMO,
as it involves that extra buffering, but then, sometimes it's the best
compromise.

>  You can interpret arbitrary context variable as if it were a template definition.

That refers to whateverString?interpret. So there you still need an
expression that evaluates to the template snippet (as a string) that
you want to run. Plus, you can't call built-ins (?thing) from Java
either... (again, for no good reason IMO, it just doesn't have any
public API apparently).

> This sounds like what I am trying to do, but I don't see how to do it.

Well, what's exactly your goal?

> Thanks.
>
> Rebecca Younes

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Centralized Desktop Delivery: Dell and VMware Reference Architecture
Simplifying enterprise desktop deployment and management using
Dell EqualLogic storage and VMware View: A highly scalable, end-to-end
client virtualization framework. Read more!
http://p.sf.net/sfu/dell-eql-dev2dev
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.