Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Velocity "main template"?

I'm very new to Apache Velocity, and I'm having a little trouble figuring out the optimal way to structure my templates. In most of the guides I have seen the pages have been built like this:

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

I have also seen someone come close to a "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 guess it works.

I've used Twirl a lot, so I might be spoiled, but I'd like to inject a template into another, essentially ending up with a main template like this:

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

And then writing every other template as:

#parse(main){ 
    TEMPLATE CONTENT 
}

Is this possible in Velocity? If yes, is it bad practice, and if so why? Thanks.

like image 631
hrmffffff Avatar asked Sep 10 '14 22:09

hrmffffff


1 Answers

Velocity itself doesn't provide good support to layout template (the main template as you called). However they provide a tool called Velocity Layout Servlet (VLS).

To give you a heads up, some other templating solution like Rythm provides very nice template layout management via the template inheritance mechanism (demo). Disclaimer: I am the author of Rythm so I might have some bias here. However you can checkout this article created by a third party to understand the pros and cons of different template solutions.

like image 133
Gelin Luo Avatar answered Oct 14 '22 02:10

Gelin Luo