Re: How to build a Custom Include method?
"Jonathan Revusky" <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Hmm, just offhand, if it were me (and assuming I understand what you want correctly) what I would do is simply create a custom f.c.TemplateLoader implementation that incorporates this extra logic. Basically, I think I I would just have it so the custom TemplateLoader applies the extra logic when the name matches a certain pattern, and otherwise, just falls back on the default logic. So, for example, suppose you just say (it's arbitrary of course) that to signal the custom include, the template name will start with # and maybe some extra info occurs at the end after a colon, say. So, you would effectively do a custom include from the template simply with: [#include "#foo.ftl:param1, param2"] So, your custom template loader sees that the template name passed in matches the pattern for special handling (i.e. it starts with #) and then applies special logic, like extracting extra info after the colon. And, of course, if you just have: [#include "foo.ftl"] it could just invoke the super method and load the template normally. Or, come to think of it, maybe it would be better instead of just a starting # to use something like: [#include "custom://foo.ftl?param1=bar¶m2=baz"] since that would sort of have a familiar feel about it. But basically, the idea is just create a custom TemplateLoader that recognizes certain patterns and does the appropriate thing in those cases. JR On Tue, Jul 29, 2008 at 10:12 PM, Jeremy Chone <[email protected]> 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) 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. > > 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, > > > > ------------------------------------------------------------------------- > 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 > > ------------------------------------------------------------------------- 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=/