I'm in love with razor templates in .NET MVC 3. Is there anything close for java?
I'd be looking for something where I could avoid using JSTL tags and instead do something like this:
<c:if test=${bla}> <span>my html</span> </c:if>
and instead do
@if(bla) { <span>my html</span> }
I'm assuming there must be similar
I'd like to introduce my work: Rythm template engine, a lightweight and super fast template engine in Java using the Razor like syntax. Rythm has rich features and supports page layout/inheritance, customized tags (either in template or java class), dynamic reload at dev mode and much more. The benchmark shows Rythm is 2 to 3 times faster than Velocity on a normal page!
The API is simple:
render with inline string:
String output = Rythm.render("@args String who;hello @who!", "world");
render with template file:
String output = Rythm.render("hello.txt", "world");
A brief introduction to Rythm: http://software-lgl.blogspot.com.au/2012/03/rythm-easy-to-use-high-performance-java.html
Updates 20120701
The latest version introduced a feature called "String Interpolation Mode", which enable you to do very lightweight string interpolation like follows:
String result = Rythm.render("hello @who!", "world");
A full feature demonstration is hosted on GAE: http://play-rythm-demo.appspot.com/
Updates 20130406
A rythm fiddle web site is online now, and you can use it to learn Rythm syntax. Check it out at http://fiddle.rythmengine.org
Updates 20130513
com.greenlaw110.rythm
to org.rythmengine
, the maven group id changed accordinglyAs far as I know, there is none that looks and behaves quite like Razor in that Java world.
From what I can understand, the templating engine in Razor does not only parse the "placeholders" in the static text, like #xxx
in Velocity or ${xxx}
in JSP.
Instead the @
symbol in Razor acts as a toggle for switching to the hosting language parser (VB and C# in case of Razor), recognizing full syntax of the element immediately following the @
sign. This allowes Razor to recognize both names of objects passed to the engine as well as syntactic structures like for loops and conditionals.
This opens up the full power of the hosting language to te Razor templates, which can be a dangerous thing in wrong hands...
Most of the templating engines in the Java side of the world have conciously chosen to strictly separate business logic from templating concerns and thus have very limited or no support for dynamic features like looping or conditionals in their template "languages", opting for declarative style over dynamic.
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