Freemarker template tips, want to create a master template

I want to create a master template that every other view page inherits.

So, the main template will have:

HEADER --CONTENT-- FOOTER 
  • the title will be displayed (if the user is logged in), the username and other properties of the user object.

  • --CONTENT-- is a placeholder that other "inheriting" view pages will insert their content into.

So my questions is, is this possible with freemarker? If so, is there any guidance?

How to pass a user object to the header from my controller actions? ideally, the object will be transmitted somewhere OTHER than each view page (to avoid the need to maintain this code on each view page).

+4
source share
2 answers

Yes it is possible. In our applications, objects such as a user object exist in the session area, but this can be any freemarker available that can be accessed:

 <#if Session.the_user?? && Session.the_user.loggedIn> <#-- header code --> </#if> 

You can omit Session. , and Freemarker will look for different areas for the given variable name.

To add content, include it at a point in the main template, where you want the view page to place its content:

 <#nested> 

Then, the browse pages announce the use of the wizard template as follows:

 <#import "/WEB-INF/ftl/path/to/template/master.ftl" as com> <@com.template> View page content </@com.template> 
+2
source

I did a Freemarker template inheritance - https://github.com/kwon37xi/freemarker-template-inheritance I think this is what you want. It is tested on freemarker 2.3.19.

+1
source

Source: https://habr.com/ru/post/1316176/


All Articles