2012-09-02 52 views
4

我一直在尋找一種像Jinja/Django的模板引擎一樣的Java模板框架。我發現似乎很受歡迎的是StringTemplate和FreeMarker,但似乎都不支持「塊」(jinja/django)。Java jinja/django-like html模板

我發現的是Jangod,它的效果很好 - 但是,根本沒有支持,它不是由任何人維護的,並且是未完成的(即沒有文檔)。

我也使用過Playframework的模板,可悲的是它與我現在開發應用程序的平臺不兼容; Google Appengine。

(TLDR;尋找一個Java模板框架,還活着,有一個塊的系統一樣神社,並能在AppEngine上的嚴格的規則運行)

+0

可能的重複:http://stackoverflow.com/questions/490390/jsp-template-inheritance –

+1

塊是一個java模板引擎,語法非常類似於jinja/django。 Chunk現在運行在GAE上:http://chunk-docs.appspot.com/ –

回答

1

看一看的節奏模板引擎:http://rythmengine.com

在jinja中的「block」功能在Rythm中被稱爲「section」。因此,假設您的佈局模板(母模板)稱爲main.html

<h1>@get("title", "default main page")</h1> 
<div id="left-panel">@render("leftPanel")<div> 
<div id="right-panel">@render("rightPanel")</div> 
<div id="main-content">@render()</div> 
<div id="footer"> 
@render("footer"){ 
    @** 
    * the content here is supplied if the child template failed 
    * to provide it's own footer implementation 
    *@ 
    <div class="footer">copyright 2012 ...</div> 
} 
</div> 

這裏是你的目標模板:

@extends(main) 
@set(title: "My Cool Page") 
@section("leftPanel") { 
<ul class="menu"> 
... 
</ul> 
} 

@section("rightPanel") { 
<div class="news"> 
... 
</div> 
} 

@*** note no "footer" section supplied so the default content will be used **@ 

@*** the rest is for the main content **@ 
... 

此功能的一個真實的演示可以在http://rythmengine.com/demo/testdefaultlayoutcontent

全面的發現文件可在http://www.playframework.org/modules/rythm找到。雖然它的目標是Play!Framework,但大部分內容也適用於沒有Play!Framework的純Rythm引擎。

您不需要擔心GAE因爲演示本身在GAE上運行。

+0

看到所有功能,它看起來非常好 - 特別是緩存實用程序,只是爲了確保;它支持嵌套的「節」嗎?支持 – Philipp

+0

嵌套部分。對於部署的版本,「ToString」功能在GAE上存在問題。我已經修好了。整個網站的源代碼可以在http://github.com/greenlaw110/rythm-website/找到。這些文檔可以在http://www.playframework.org/modules/rythm找到 –