Re: including other FTL-template through model

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Thursday, February 5, 2009, 3:19:59 PM, aldana wrote:
>
> Hi,
>
> usually to include other FTL I need to use <#include> directive. For special
> dynamic inclusion though I would like to do this directly through the
> backing Model java-class. 
>
> Is this possible in freemarker? (e.g. Tapestry5 supports building up page
> both in template file and java-type)

Certainly it's possible.

Where do the source code of those dynamically included templates come
from? When is it decided what their source code will be?

(a) If their source code is known before building the application,
then you could store them just like regular templates, and then pass
the *name* (the path) of those template with the data-model instead of
the content of template. Then you could just use
<#include theTemplateName>, where theTemplateName is a variable in the
data-model.

(b) If the source code of these templates is totally ad-hoc, then you
can pass the source code with the data-model as a string, and then use
<@theTemplateSourceCode?interpret />, where theTemplateSourceCode is a
variable in the data-model. But beware, in this case you will *parse*
the source code each time you execute that directive, which is much
more resource intensive than just executing an already parsed (cached)
template. But if the template is to be executed only once, because
it's totally ad-hoc, you don't have better alternative anyway.

(c) If it's something between (a) and (b), i.e., you don't know ahead
the template source codes, but when you do, you will execute the same
template for multiple times, then either ...

- you use a TemplateLoader that loads templates directly from the RAM
  (not from a file or other persistent storage), and that way you fall
  back to (a),

- or you use your own caching scheme, by creating Template objects
  with the constructor Template(name, sourceCodeReader, cfg), and then
  put the Template object into the data-model, and then invoke them
  using a custom directive, something like
  <@my.processTemplate theTemplate>, where theTemplate is a variable
  in the data-model.

> thanks.
>
> -----
> manuel aldana
> aldana((at))gmx.de
> software-engineering blog: http://www.aldana-online.de

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
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.