Apache Velocity "master template"?

I am very new to Apache Velocity, and I have a little problem finding the best way to structure my templates. In most manuals, I saw that the pages were structured as follows:

#parse("header.vm") <body> ... </body> #parse("footer.vm") 

I also saw that someone came close to the "main" template with this approach:

 <head> ... </head> <body> #if($activeTab=="home") #parse("home.vm") #elseif($activeTab=="aboutus") #parse("aboutus.vm") ...and so on. </body> 

Which seems a little silly, but I think it works.

I used Twirl a lot, so I could be messed up, but I would like to add a template to another, basically getting the main template:

 <head> ... </head> <body> $content </body> 

And then writing each other pattern as:

 #parse(main){ TEMPLATE CONTENT } 

Is it possible in speed? If yes, then this is bad practice, and if so, why? Thank you

+5
source share
2 answers

You can use the variable $!bodyContent .

mainLayout.vm:

 #macro(mainLayout) <head> ... </head> <body> $!bodyContent </body> #end 

index.vm:

 #@mainLayout() <h1>Index page</h1> #end 
+3
source

The speed itself does not provide good support for the layout template (the main template that you called). However, they do provide a tool called Servlet Layout Servlet (VLS).

To give you an edge, some other template solution, such as Rythm, provides very good template layout management using the template inheritance mechanism ( demo ). Disclaimer: I am the author of Rythm, so I may have a bias. However, you can check out this third-party article to understand the pros and cons of various template solutions.

+5
source

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


All Articles