开发者

Is there a templating language similar to Razor for Java Spring web applications?

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</开发者_JAVA技巧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:

  1. render with inline string:

    String output = Rythm.render("@args String who;hello @who!", "world");

  2. 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

  • package name changed from com.greenlaw110.rythm to org.rythmengine, the maven group id changed accordingly
  • Checkout the new project website: http://rythmengine.org


As 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.


Spring supports a number of templating languages:

  • Velocity (as mentioned in another answer)
  • Freemarker
  • Good ole' JSP, which if you use taglibs and EL (Expression Language) is basically a template engine

For more reading on how to integrate with Spring, see this page.

Additionally, there's StringTemplate, which while not referenced in the official Spring documentation, can be used as a Spring template engine.

And if you really want to go wild, here's a page with about a bazillion other template engines.


Take a look at twirl, the Play framework template engine separated from the framework.

Example:

@if(items.isEmpty) {
  <h1>Nothing to display</h1>
} else {
  <h1>@items.size items!</h1>
}


Try having a look at velocity (http://velocity.apache.org)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜