0

I'm desperately looking for a Java HTML view engine that fulfills three main requirements:

  1. Support for master template pages.
  2. HTML templates can be used as subviews within other templates.
  3. HTML templates do not have to be backed by config files or Java classes.

It's for a Java web application that mainly consists of read-only page and a few pages with forms. Most likely I'll use it in combination with an MVC framework.

Master Template Pages

The main structure of the HTML should be defined by a master HTML page. The different pages just provide the core content that's put into the master page to create the final page. This is more than just the inclusion of header and footer.

Subviews

A page should be able to use other HTML pages/templates as subviews within its own content. It should be able to pass at least one parameter to provide the data that the subview needs to display. Furthermore, it should be possible to recursively use further subviews within a subview. Again, this goes beyond a simple include mechanism.

No Backing

HTML templates should consist of a single file that basically is an HTML or XML page where certain parts will be substituted based on the provided data. They shouldn't need any additional (per template) config files. And they shouldn't have the need to implement any Java classes for backing.

I've already had a look at many Java template engine. But neither of them seems to meet these requirements. (In the .NET world, ASP.NET MVC with the Razor view engine would be a perfect fit though.)

Update:

So far, I've looked at the following engines (please let me know if I've overlooked a way to achieve my requirements with one of these engines):

  • JSP: Has just a very basic inclusion mechanism without master templates or proper scoping for subviews etc.
  • Velocity: Has a slightly advanced inclusion mechanism, but no master pages.
  • FreeMarker: Include mechanism, no master pages.
  • Tapestry: Good component based subviews as well as a templating mechanism. However, it doesn't seem to be easily possible to use template engine part without the rest of the framework, which is too page-centric to be combined with an MVC framework.
  • Tiles: Requires two jsp pages per final page. The two layers (tiles and jsp) makes it too complex, in particular for subviews.

Update (2): I've changed some term: view engine instead of template engine, subviews instead of components.

Codo
  • 75,595
  • 17
  • 168
  • 206
  • 1
    It'd be easier if you listed which engines (which is it, "many", or "neither"?) you've looked at, and why they don't meet your requirements, to avoid the canonical suggestions. – Dave Newton Dec 31 '11 at 14:12
  • I think Tiles should satisfy your needs. – JB Nizet Dec 31 '11 at 14:19
  • JSP allows halfway-decent templating, actually, but scoping can be a pain. Velocity/FreeMarker both allow templating via inclusions. Tiles, particularly recent versions, more than satisfies the requirements, although it's configured via XML (there may be other config mechanisms available, though). SiteMesh is a bit different and may not be what you're looking for. I think Mustache and MVEL are both out, but might be worth a look. – Dave Newton Dec 31 '11 at 14:37
  • @DaveNewton: Thanks for your short analysis. I had a look at Tiles, and I don't like it (see the update to my question). – Codo Jan 01 '12 at 10:13

1 Answers1

0

You haven't really looked at template engines. JSP, Velocity and FreeMarker are not template engines (with your meaning of a template engine). They're languages allowing to generate markup dynamically, and get the data to display in the generated HTML from Java objects. Tapestry is a complete web application framework, based on components.

If you're using JSP to generate the HTML pages, you can use a template engine on top of JSP like Tiles or SIteMesh, which will handle the templating, and thus allow to have one JSP per "component" of a full page. JSP should generaly not be used without an web MVC framework like Stripes, Spring MVC or Struts2. All of these either have their own templating support, and/or support integrating another one like SiteMesh or Tiles.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I agree that the term "template engine" might not be the best to describe the JSP, Velocity etc. I did have a look at Tiles and I'm not sure if I properly understand how it works. It seems to be an extra layer that composes parts of a page. That's fine for master pages. However, it doesn't seem to provide components. Or how would it be possible to build an HTML table with some sort of _for_ loop in a JSP page and then include another JSP page to fill a table cell in each row? How could Tiles help here? – Codo Dec 31 '11 at 14:57
  • Tiles can be nested inside other tiles. See http://tiles.apache.org/framework/tutorial/advanced/nesting-extending.html. To do what you just described, a simple include or the use of a JSP tag file is probably sufficient, though. But Tiles will also allow it if you want. – JB Nizet Dec 31 '11 at 15:04
  • I have seen that page about nesting. But it seems to work only for a fixed structure and not for a table with a varying number of rows. Or didn't I properly grasp the concept of Tiles? – Codo Dec 31 '11 at 15:10
  • You may loop with JSP tags, and at each iteration, insert one (or several) attribute(s) of your tile definition. Tiles definitions tell which layout page to use, and what to include in each placeholder of the layout. The looping is not done with Tiles. It's done with JSP tags. – JB Nizet Dec 31 '11 at 15:14
  • Thanks, I'll give it a try though the use of components does seem to be so easy and elegant as one would wish. – Codo Dec 31 '11 at 15:32
  • If you want to use components, then opt for a component-based framework like Wicket, JSF, or Tapestry. But make sure to understand what components mean, first. – JB Nizet Dec 31 '11 at 15:35
  • Component isn't probably the best word. I've updated my question to use the term subview. Then I had a closer look at Tiles but found just too complex: you have to create two jsp pages for each final page just to use a master templates. And using another jsp page as a subview isn't easy either. – Codo Jan 01 '12 at 10:15
  • Tiles definitions are typically defined in an XML file, and you just have one JSP for the layout, one JSP for every part, and the XML file assembling the final pages. No need for 2 JSP pages per final page. – JB Nizet Jan 01 '12 at 10:39