4

I'm developing a Java-servlet based project and I'm a newbie in this platform. I usually use Django, that has an awesome default template engine. Is there something similar for Java?

PS: Template inheritance is a must (to prevent repetition). There are several libraries like mustache which do not give this feature.

  • What do you mean by "template inheritance"? – Bob Dalgleish Oct 22 '14 at 12:04
  • Let's say you have some portion of your HTML that repeats a lot, for example a navigation bar coupled with a footer/sidebar etc. Now, you put your repetitive part in a file, say base.html and at the place where content is not same for all files, you say {{block body}}. Then, in another file, say login.html, if you're using template inheritance, you only need to say {extends base.html} and then {{start bodyblock}} Your new content {{end bodyblock}}. This will render the static as well as dynamic content. You can invoke any file from another file. – Ranveer Oct 22 '14 at 12:18
  • Better explanation here: probablyprogramming.com/2008/03/10/django-template-inheritance – Ranveer Oct 22 '14 at 12:19
  • In practice, how does this differ from being able to freely import page fragments, such as you can do in Freemarker Templates, or the Play framework? – Bob Dalgleish Oct 22 '14 at 13:15
  • 1
    I don't see the point of a hierarchy if you can include arbitrary constructs. Inheritance is not always the best way to go. I suggest you should consider composition, which will widen the templating resources available to you. – Bob Dalgleish Oct 22 '14 at 13:48
2

Rythm Template Engine should be exactly the thing you want. Not sure why you said the doc is not up to the mark.

Check out http://rythmengine.org/doc/template_guide.md#inheritance and play with the inheritance feature at http://fiddle.rythmengine.org/#/editor/886606b3a7034088b991855bef8f89da

Disclaimer: I am the author of Rythm

  • Back then, I actually ended up using Rythm, because I couldn't found any other templating engine as easy to use and versatile as it. Hats off for developing it! About the documentation thing, there was some issue with my-ISP-your-server that didn't allow me to view the whole page. Sorry for not editing the question sooner – Ranveer Mar 29 '15 at 9:54
  • Glad to know about that :-) – Gelin Luo Mar 29 '15 at 10:07
1

First you might want to have a read of This SO article that talks about the templating engines that are there and why to use which of them, that should get you started.

I personally have used the tiles framework and was very satisfied with it. You write your snippets and have a template to combine the parts (header, footer, navi, whatever) in always the same way.

The different tiles are regular JSP files and thus the integration is simple if you are used to JSP (which you will be if you work with Java, Servlets and JSP shortly)

I should notice that my experience with this is a bit dusted :) I didn't code for the web in years.

0

Chunk Templates provides support for this type of thing with {% exec %} like so:

base_template.chtml

<html>
  <head>
  </head>
  <body>
    {$body}
  </body>
</html>

widget_detail.chtml

{% exec base_template %}
  {$body=}
    ...
  {=}
{% endexec %}

Here's a more interesting example where the base template provides some defaults that you can override from the servlet or in an exec:

base_template2.chtml

<html>
  <head>
    <title>{$page_title:Widget Emporium}</title>
  </head>
  <body>
    {$top_nav:.include top_nav}
    {$left_nav:.include left_nav}
    {$body}
    {$footer:.include footer}
  </body>
</html>

top_nav.chtml

<div class="top_nav">
  ...
</div>

left_nav.chtml

<div class="left_nav">
  ...
<div>

footer.chtml

<div class="footer">
  ...
<div>

special_page.chtml - suppress all nav and use alternate footer

{% exec base_template2 %}
  {$body=}
    ...
  {=}
  {$page_title = Special Page}
  {$top_nav=}{=}
  {$left_nav=}{=}
  {$footer=}{% include special_footer %}{=}
{% endexec %}

exec also supports json and xml for tag value assignment.

For simpler things, Chunk syntax is quite similar to Django/jinja2. Filters are applied with pipes in the same way: {$tag|filter} and Chunk tags are written as {$tag} or {% $tag %} rather than {{ tag }} but you get used to it pretty quickly.

If-else branching in particular should look very familiar to anyone coming from Django:

{% if (...) %}
  true-case
{% else %}
  false-case
{% endif %}

Disclaimer: I am the author/maintainer for Chunk. The project is open-source and on GitHub.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.