Master page using freemarker template and J2EE like .net
Asked Answered
C

1

9

I am planning to create a web application using J2ee, Spring 3.0n, Freemarker and jQuery.

My question is: is there any way to create master page with header and footer and included all Javascript files so that i can directly call that master page in all my page and save time to include all js file again and again?

Same as .Net provides concept of a master page, I want to create my own master page in Freemarker.

Campbellbannerman answered 14/4, 2011 at 18:35 Comment(0)
G
11

Basically you write a macro, let's call it masterTemplate.

[#macro masterTemplate title="defaultTitle"]
    <!DOCTYPE html
            PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
        <title>${title}</title>
        ... scripts, style sheets, meta information ...
    </head>
    <body>
     <div id="header">...</div>
     <div id="content">
       [#nested /]
    </div>
    <div id="footer>...</div>
    </body>
    </html>
[/#macro]

Then, you use this macro within your pages like this:

[#import "/path/to/masterTemplate.ftl" as layout /]

[@layout.masterTemplate title="My test page"]
    ...content goes here...
[/@layout.masterTemplate]

You achieve some sort of decorating technique by passing all relevant data from the page as attribute to the masterTemplate: See the title attribute. In the same way you can pass additional scripts and stylesheets.

This technique is shown here: Freemarker wiki

Gavette answered 19/4, 2011 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.