I want to create a master template that every other view page will inherit.
So the master template will have:
HEADER
--CONTENT--
FOOTER
the header will optionally show (if the user is logged in), the username and other user object properties.
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).
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.
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.
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With