开发者_JAVA百科
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this questionI'm looking for the templates engine for Java with syntax like in Django templates or Twig (PHP). Does it exists?
Update: The target is to have same templates files for different languages.
<html>
{{head}}
{{ var|escape }}
{{body}}
</html>
can be rendered from python (Django) code as well as from PHP, using Twig. I'm looking for Java solution.
Any other templates system available in Java, PHP and python is suitable.
I've developed Jtwig. You could give a try. It's being used in some projects with success. It's easy to setup with a nice integration with spring webmvc.
Just include the dependency using maven or a similar system.
<dependency>
<groupId>com.lyncode</groupId>
<artifactId>jtwig-spring</artifactId>
<version>2.0.3</version>
</dependency>
And configure the view resolver bean to return the Jtwig one.
@Bean
public ViewResolver viewResolver() {
JtwigViewResolver viewResolver = new JtwigViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".twig");
return viewResolver;
}
Or, if you use xml base configurations:
<bean id="viewResolver" class="com.lyncode.jtwig.mvc.JtwigViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".twig"/>
</bean>
- http://www.jangod.org/ (There is now also https://github.com/HubSpot/jinjava)
- run django via jython on jvm
- use http://mustache.github.com/
If you want the same templates for different languages, you might take a look at Clearsilver.
Clearsilver is a language-neutral template engine which helps separate presentation from code by inserting a language-neutral hierarchial data format (HDF) between your code and templates. Think of HDF like XML, but much simpler.
It's is used for many high-traffic websites, including Yahoo! Groups, Gmail Static HTML, orkut.com, wunderground.com, and others. Implementation languages used with it include C/C++, Python, Java, Ruby, PHP, C#, and others. The Python framework also includes a Page-Class dispatcher and simple ORM which is a bit Ruby-On-Rails like in that it makes mapping between database tables, HDF, and templates take very little code.
The main Clearsilver implementation is in C with language-specific wrappers. There is also a 100% java implementation made by Google and open-sourced called JSilver.
http://www.clearsilver.net/
http://code.google.com/p/jsilver/
Sure, there are all sorts of template engines for Java. I've used FreeMarker, Velocity and StringTemplate. I'm not sure what you mean by Django-like syntax; each engine has it's own variations on a templating approach.
For a comparison of some different engines check out here.
You can use Mustache.java and Handlebars.java. Mustache is very minimalistic. Handlebars is similar and compatible with Mustache, but you can very easily write your own extensions.
精彩评论