Re: Multiple layout section override ?

Daniel Dekany <[email protected]> Sat, 9 Mar 2013 01:53:15 +0100
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Wednesday, March 6, 2013, 2:46:26 PM, Albert Kam wrote:

> I am aware that i can use macros for layouting, for example, a main
> layout that contains header, left body section, center body section, footer.
>
> Based on the examples i have googled, like this one here
> http://wendefer.blogspot.com/2007/09/handy-tip-for-layouts-in-freemarker.html,
> i can use <#nested> in the main layout for dynamic values for other
> templates that use this main layout macro.
>
> But in my case, i would like the other templates to be able to
> 'override' more than 1 <#nested>, for example, providing the content
> of left body section -and- center body section (like apache tiles).
>
> Is this supported, or maybe is there any other approaches to do this ?

Sadly, there's still no built-in solution for this. I have seen two
approaches to work this around. First is passing macros to the layout
macro:

  <#macro myHeading>
    ...
  </#macro>

  <#macro myFooter>
    ...
  </#macro>

  <@myApp.page heading=myHeading body=myFooter>
    ... this is the body ...
  </@myApp.page>

It's kind of noisy though. So I would rather implement this (some
deeper FreeMarker knowledge is needed):

  <@myApp.page>
    <@block name="heading">
      ...
    </@block>
    <@block name="body">
      ...
    </@block>
    <@block name="footer">
      ...
    </@block>
  </@myApp.page>

Here @block has to be implemented by you, in Java
(TemplateDirectiveModel). The layout macro need to push a
TemplateHashModel into a stack that's stored in a custom Environment
attribute, then call #nested. Then, @block should capture the content
generated inside its body, get the topmost TemplateHashModel from said
stack, and put the captured content into with block name (like
"heading") as the key.

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Symantec Endpoint Protection 12 positioned as A LEADER in The Forrester  
Wave(TM): Endpoint Security, Q1 2013 and "remains a good choice" in the  
endpoint security space. For insight on selecting the right partner to 
tackle endpoint security challenges, access the full report. 
http://p.sf.net/sfu/symantec-dev2dev