Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

advice on freemarker templating, want to create a master template

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).

like image 698
Blankman Avatar asked Jul 19 '10 14:07

Blankman


People also ask

What is FreeMarker Template Error?

FreeMarker template error appears in logs when attempting to add a Web Content Article to a page or search for it in the Search portlet. The issue only occurs when the Summary field is empty and when using the following line in the template.

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 create an FTL file in eclipse?

Create a new folder called lib and add the Freemarker library to it. Add this library to the classpath for your project. If you don't know how to achieve that, please see the Eclipse IDE Tutorial for instructions on the required steps. Create a new folder called templates inside the folder of the com.


2 Answers

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>
like image 67
Pat Avatar answered Nov 15 '22 00:11

Pat


I made Freemarker template inheritance - https://github.com/kwon37xi/freemarker-template-inheritance I think it's what you want. It is tested on freemarker 2.3.19.

like image 21
KwonNam Avatar answered Nov 14 '22 22:11

KwonNam