Yes, I know that this has been asked before. But most of the questions were two years ago, and there's got to be a better answer today.
I need a template language for rendering HTML. Requirements:
Must be callable from Java.
Must not be Freemarker, Velocity, or StringTemplate. We've been using Freemarker for a year, and it's too slow. It consumes 50% of our CPU cycles under load. Velocity is worse than Freemarker, and StringTemplate has got its own issues.
It must consume JSON, or its Java equivalent, i.e. Maps, Lists, and primitives.
I'm starting to get Node.js envy. There has been a ton of template activity for Javascript in the last year, but little for Java (that I know of).
The ideal language would look like Liquid, which exists only for Rails.
Jade exists for Java, but I don't want all the HTML reformatting that comes with it.
Update
I eventually settled on Handlebars, which is also available for Java. Both work great.
Chunk is json-friendly. JSON can be used as a tag value in your controller code or in the templates themselves for exec/macro calls.
{% exec %}
{% data @json %}
{ name: "whatever",
vitals: ["an","array","of","data"],
friends: [{name: "bob"},{name: "crystal"}]
}
{% enddata %}
<div>Name: {$name}</div>
{% if ($friends) %}
<div>Friends:
<ul>
{% loop in $friends as $friend %}
<li>{$friend.name}</li>
{% endloop %}
</ul>
</div>
{% endif %}
{% endexec %}
Or, just use the inner template and inject the json from the java side.
src/themes/example.chtml
<div>Name: {$name}</div>
{% if ($friends) %}
<div>Friends:
<ul>
{% loop in $friends as $friend %}
<li>{$friend.name}</li>
{% endloop %}
</ul>
</div>
{% endif %}
MyController.java
Theme theme = new Theme();
Chunk html = theme.makeChunk("example");
html.set("name", "whatever");
html.set("vitals", getJsonArray() );
html.set("friends", getJsonFriendObjects() );
html.render( out );
As long as the getJsonXXX() methods return something that implements List and Map, Chunk will glue it into the template correctly (even if those Lists and Maps nest more Lists and Maps).
Output:
<div>Name: whatever</div>
<div>Friends:
<ul>
<li>bob</li>
<li>crystal</li>
</ul>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With