Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Master page using freemarker template and J2EE like .net

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.

like image 323
Deepak Kumar Avatar asked Apr 14 '11 18:04

Deepak Kumar


People also ask

What is the use of FreeMarker template?

FreeMarker is a template engine, written in Java, and maintained by the Apache Foundation. We can use the FreeMarker Template Language, also known as FTL, to generate many text-based formats like web pages, email, or XML files.

How do I use FreeMarker template in spring boot?

Freemarker Template FilesSpring boot looks for the The template files under classpath:/templates/. And for the file extension, you should name the files with . ftlh suffix since Spring Boot 2.2. If you plan on changing the location or the file extension, you should use the following configurations.

What syntax is used to access data using FreeMarker under a condition?

To access them, you use the . variable_name syntax.

What is HTML in FreeMarker?

FreeMarker is a tool that generates text output based on templates. FreeMarker syntax lets you add dynamic variables to your HTML text that permit customized output. These variables provide more flexibility and customization than CRMSDK tags that are used in version 1 templates.


1 Answers

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

like image 116
Hubert Avatar answered Oct 16 '22 15:10

Hubert