Re: How to build a Custom Include method?
"Jeremy Chone" <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Thanks all for the response. This helps a lot, I think I have everything I need. Here are my quick answers to some of your points: To JR about TemplateLoader: Yes, usually the right way of doing this would be a template loader. However, in my case the template can depend on many other states that cannot easily be encoded in a simple string. For example, I might choose a different template depending of the httpSession states (or cookies values) or from where the template is being included included. To Attila about getEnvironment: This is perfect. I will get the writer from the Environment. To Daniel about TemplateDirectiveModel: Cool, makes total sense, TemplateDirectiveModel is what I need. Is that equivalent to creating a macro? I assume I use the Configuration.setSharedVariable method. Jeremy, On Wed, Jul 30, 2008 at 3:54 AM, Daniel Dekany <[email protected]> wrote: > 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=/ _______________________________________________ FreeMarker-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freemarker-user