5

Tiles and Sitemesh look quite popular but this stuff is really old and look terrible compared to current awesome stuff from e.g. Ruby (ERB) or PHP (Open Power Template). These days template engines allow comfortable templating (inserting variables, autoescaping depending on the context, iterating through Iterables, accessing bean properties) and layouting (e.g. headers, footers and overriding and adding to some parts defined in parent) without any difficult configuration and wihout a need for changing your current stack (e.g. your web framework).

Example parent.html:

<html>
  <head>
    <title>
      <layout:part name="title">
        Default title
      </layout:part>
    </title>
    <layout:part name="head" />
  </head>
  <body>
    <div class="menu" layout:part="menu">
      default menu
    </div>
    <div class="content" layout:part="content" />
    <div class="footer">
      (c) me
    </div>       
  </body>
</html>

Example child.html

<layout:extend file="parent.html">
  <layout:fill name="title">
    Custom title
  </layout:fill>
  <layout:fill name="contnet">
    the content
    {$var} from model
  </layout:fill>
</layout:extend>

I'm looking for better Facelets, that won't require me to change the whole stack - I'm not going to adapt the whole project to JSF or Wicket just to use better views.

The template engine should not require any additional servlets or filters (no URL-based logic). I want to use the engine programatically. A possible use case is defining a custom ViewResolver in Spring 3.

It would be perfect if layouts weren't defined up-front in a config file. That's not needed if you just define the parent view in the template file.

The framework may be on top of JSP but doesn't have to. The advantage is a possibility to use taglibs provided by other frameworks (e.g. Spring).

Or maybe everything is already there in Sitemesh/Tiles but needs lots of configuration? If you know of any example configuration that allows to achieve all the mentioned goals, let me know about it.

Related question: what alternatives exist to Sitemesh to help layout JSP/JSTL page footers/headers in a Spring MVC app? - my question refers to templating too, and is not limited to Spring Web MVC.

Community
  • 1
  • 1
Nowaker
  • 12,154
  • 4
  • 56
  • 62
  • Why do you think Tiles 2 is old styled? I've just overlooked ERB, but it seems that you can achieve same features using Tiles 2 and Velocity, for example. What do you need? – sinuhepop May 14 '12 at 08:50
  • Asking for framework suggestions is discouraged here. If you want to [edit] out all requests for such, and make this a question that can be answered here without clicking off-site, please do so and flag for reopening. Thanks. –  May 14 '12 at 17:23

1 Answers1

4

I have always supported the idea that JSP is a good-enough view technology that is also usable for templates (using includes)

For programmatic handling I use velocity, which is rather simple and straightforward.

The best view technology I've encountered in the Java world is grails' GSP, but you might need to migrate your whole web layer to grails, which is not always an option.

Just a final note - whatever you do, do not use freemarker. It is unnecessarily complicated and you can't easily achieve simple tasks.

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 3
    I read this before. I have to admit that having a complicated stack consisting of Sitemesh, Tiles, Velocity and lots of configuration files is not good. However, simple JSPs help you nothing with layouting and are quite weak with templating. Being used to good template engines from Ruby, PHP, it's quite frustrating not to have the equivalent in Java - the stack I work on most of the time. :( – Nowaker Feb 25 '12 at 22:33
  • 1
    grails is good for that - (g)rails – Bozho Feb 25 '12 at 22:34
  • 3
    This is whole stack. I can't see a reason why developers should migrate existing project to JSF just to use Facelets, to Grails just to use GSP, and so on. – Nowaker Feb 25 '12 at 23:02
  • all the good stuff is in the Javascript UI (widget) framework – Tom May 08 '12 at 17:30
  • BTW GSP is now standalone, and can be used outside of grails. so you dont need to migrate whole app to grails – Sudhir N Aug 23 '16 at 14:49