Re: How to build a Custom Include method?

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Wednesday, July 30, 2008, 7:12:31 AM, Jeremy Chone wrote:

> Hi,
>
> I would like to build a custom include method. Basically, I would
> like to be able to call something like: 
>
> ${customInclude(param1,param2)} 
>
> which will include a template almost as a traditional [#include
> does. There are two main differences between this customInclude and
> a traditional freemarker include: 
>
> 1) customInclude add another level of indirection to get to the
> template name (handled by java business objects)
>
> 2) Each customInclude might have a different model. 
>
> Today, I have an implementation that works. Basically, I
> implemented an TemplateMethodModelEx method (customInclude)

It should be a TemplateDirectiveModel, not a TemplateMethodModelEx.
Thus it is to be called as <@customInclude param1 param2 />, not as
${customInclude param1 param2}. Directives are for output
progressively and without escaping. Interpolations (${...}) are to
insert the result of simpler expressions, possibly escaped. Anything
you do with ${...} should not have a side-effect, so it should fiddle
with the output Writer. Like consider what would happen with your
solution here: <#assign s = "The output is: ${customInclude(...)} ">.
It wouldn't work correctly, "s" would be "The output is: ", etc.

> which implements the logic to get the template name, and get the
> "Writer" object from the param. Then, I just get the template and
> call template.process with the givent Writer.

(The Writer can be get as the others has already told... your
customInclude method could be called directly by the users.)

> It's ugly, but it work. I hide this method with a macro, so, as the
> pages do not have too now about the Writer object/param. 
>
> Is there better way for doing this? Can I get some more context in a TemplateMethodModelEx method ?
>
>
> Jeremy, 

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