including other FTL-template through model

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

including other FTL-template through model

aldana-2
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)

thanks.
Reply | Threaded
Open this post in threaded view
|

Re: including other FTL-template through model

Daniel Dekany
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
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: including other FTL-template through model

aldana-2

Daniel Dekany wrote
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) [...] <#include theTemplateName>, where theTemplateName is a variable in the
data-model.
Thanks, I already went for the (a) option (great that #include directive offers string expression), so it seems that freemarker first replaces the variables and text strings and after that goes to process the directives and macros.
Reply | Threaded
Open this post in threaded view
|

Re: including other FTL-template through model

Daniel Dekany
Thursday, February 5, 2009, 11:28:26 PM, aldana wrote:

>
> Daniel Dekany wrote:
>>
>> 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) [...] <#include theTemplateName>, where theTemplateName is a variable
>> in the
>> data-model.
>>
>>
>
> Thanks, I already went for the (a) option (great that #include directive
> offers string expression),

It's just like in Java or any other widely used programming
languages... a parameter can be any kind of expression, as far as the
end result is of the proper type.

> so it seems that freemarker first replaces the
> variables

and calculates with the operators too, like in the case of
<#include userBaseDir + "menu.ftl">

> and text strings

(a text string represents a final value, so there is nothing to
replace there)

> and after that goes to process the directives and macros.

Almost, but not exactly. Just before a directive or method is
executed, all parameters of it is calculated. Not earlier than that.
And if the same directive or method call is executed later again, then
the parameters will be calculated again. So for example, this:

  <#list ["a.ftl", "b.ftl", "c.ftl"] as templateName>
    <#include templateName>
  </#list>

does the same as

    <#include "a.ftl">
    <#include "b.ftl">
    <#include "c.ftl">

Well, just like in most other languages.

> -----
> 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
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user