views:

44

answers:

1

I want to create a master template that every other view page will inherit.

So the master template will have:

HEADER
--CONTENT--
FOOTER
  1. the header will optionally show (if the user is logged in), the username and other user object properties.

  2. the --CONTENT-- is a placeholder that other 'inheriting' view pages will inject their content into.

So my questions are, is this possible with freemarker? If so, any guidance?

How would I pass the user object to the header from my controller actions? ideally the object would be passed in somewhere OTHER than each and every view page (to avoid having to maintain this code on each and every view page).

+1  A: 

Yes, it's possible. In our applications things like the user object exist in session scope, but this could be any scope freemarker has access to:

<#if Session.the_user?? && Session.the_user.loggedIn>
    <#-- header code -->
</#if> 

You can omit the Session. and Freemarker will search the various scopes for the given variable name.

To inject the content, include this at the point in the master template where you'd like the view page to put its content:

<#nested>

The view pages then declare their use of the master template as follows:

<#import "/WEB-INF/ftl/path/to/template/master.ftl" as com>
<@com.template>
    View page content
</@com.template>
Pat
I'm using spring MVC, and I will load the User object if the user is logged in during the event that fires before ALL action methods. Where should I store the user object then?
Blankman
If it fires before every action then it sounds like the request scope is a good candidate for holding the user object.
Pat
so request.attributeS?
Blankman
Yep, Request.attributeName should do the trick.
Pat