12
votes

My website has a consistant header and footer that I want on all pages.

What is the best way to do this?

The header will have some dynamic data also, based on the current view.

P.S Does freemarker have any sort of master page functionality? Where I can create a base template, then have other templates build upon the base?

Basically I want to design a template that has header and footer + a place holder for the main content area. THEN, all other pages will inherit the main template (with header + footer), and then inject the action's render output into the templates main content area.

3

3 Answers

17
votes

Define a macro like this in an import library:

<#macro page title>
    <html><head><title>${title?html}</title></head>
    <body>
    <!-- header section -->
    <#nested/>
    <!-- footer section -->
    </body></html>
</#macro>

and use the following template for all your pages:

<#import "common.ftl" as c/>
<@c.page title="custom page title">
    <!-- custom page content -->
</@c.page>
1
votes

You can find the spring-specific part in the spring reference and the freemarker part in the freemarker online docs

(It doesn't seem like Freemarker supports master pages, but through recursive use of include you can achieve a high level of code reuse)

1
votes

Have a look at SiteMesh. I believe you will find it useful.