Re: How to build a Custom Include method?
Jeremy Chone <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Sorry for the very delay replied. Anyway, I can't wait to upgrade my code to Freemarker 2.4.x, so, I will manage the x=.. for now. On Fri, Aug 1, 2008 at 2:04 AM, Daniel Dekany <[email protected]> wrote: > Wednesday, July 30, 2008, 7:12:09 PM, Jeremy Chone wrote: > > > 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 should be, but in 2.3.x positional parameter passing (like > <@foo 1 2 /> instead of <@foo x=1 y=2 />) is not supported for them, > as the parameters are received with a Map. Starting from 2.4 it's solved > with Java 5 annotations. I think it would really be good to backport > that... but as we can't require Java 5 for 2.3.x, it would be tricky. > But really, Jonathan, can't we add an interface for the same purpose? > Then it would be usable in 2.3.x as well. > > > I assume I use the Configuration.setSharedVariable method. > > That's a possible approach. Another one is to put it into an > #import-able/#include-able file, like if it were a macro. That's > possible as <#assign foo = "com.example.FooDirective"?new()>. > > > 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 > > > > > > > > -- > Best regards, > Daniel Dekany > > ------------------------------------------------------------------------------ This SF.net email is sponsored by: SourcForge Community SourceForge wants to tell your story. http://p.sf.net/sfu/sf-spreadtheword _______________________________________________ FreeMarker-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freemarker-user