Mustache.java 的設計目的不是允許不受信任的各方提供模板。可以將其鎖定以安全地提供此服務,但預設情況下它是不安全的。使用 SafeMustacheFactory 並將所有模板和部分列入白名單。
從版本 0.9.0 開始,mustache.java 現在只適用於 Java 8。對於 Java 6/7 支持,請使用 0.8.x。
沒有外部依賴項,編譯器函式庫約為 100k。
Mustache.java是 Mustache.js 的衍生版本。
有一個用於支援和提問的 Google 群組:http://groups.google.com/group/mustachejava
Github CI:https://github.com/spullara/mustache.java/actions/workflows/maven.yml
API文件:http://spullara.github.io/mustache/apidocs/
Mustache.java 最大的生產部署:
感謝 YourKit 的許多效能改進:
YourKit 以其全功能的 Java Profiler 熱忱支援 Mustache.java 開源專案。 YourKit, LLC 是用於分析 Java 和 .NET 應用程式的創新智慧工具的創建者。看看YourKit領先的軟體產品:
捐款請求:
文件:
mustache
規格測試模空白差異Iterable
都可以用於類似列表的行為ExecutorService
則傳回Callable
允許並發評估{{<super}}{{$content}}...{{/content}}{{/super}}
){{#func1}}...{{/func1}}
)是使用 Java 8 中的Function
實現的(替換後)TemplateFunction
handlebar
伺服器將渲染模板+json數據,以便設計師快速製作模板模型CapturingMustacheVisitor
從即時系統中提取範例資料進行模擬和測試invert
調用可以採用文字和模板並求解數據表現:
compiler
模組中的com.github.mustachejavabenchmarks
包indy
模組使用 codegen 模組和 invokedynamic 將模板編譯為字節碼建構建議:
Maven 依賴資訊(即,對於大多數常見情況,您只需要compiler
模組):
Java 8+:
< dependency >
< groupId >com.github.spullara.mustache.java</ groupId >
< artifactId >compiler</ artifactId >
< version >0.9.10</ version >
</ dependency >
Java 6/7:
< dependency >
< groupId >com.github.spullara.mustache.java</ groupId >
< artifactId >compiler</ artifactId >
< version >0.8.18</ version >
</ dependency >
範本檔案範例:
{{#items}}
Name: {{name}}
Price: {{price}}
{{#features}}
Feature: {{description}}
{{/features}}
{{/items}}
可能由一些支援程式碼提供支援:
public class Context {
List < Item > items () {
return Arrays . asList (
new Item ( "Item 1" , "$19.99" , Arrays . asList ( new Feature ( "New!" ), new Feature ( "Awesome!" ))),
new Item ( "Item 2" , "$29.99" , Arrays . asList ( new Feature ( "Old." ), new Feature ( "Ugly." )))
);
}
static class Item {
Item ( String name , String price , List < Feature > features ) {
this . name = name ;
this . price = price ;
this . features = features ;
}
String name , price ;
List < Feature > features ;
}
static class Feature {
Feature ( String description ) {
this . description = description ;
}
String description ;
}
}
並會導致:
Name: Item 1
Price: $19.99
Feature: New!
Feature: Awesome!
Name: Item 2
Price: $29.99
Feature: Old.
Feature: Ugly.
模板的評估連續進行。例如,如果您的回呼之一中有阻塞代碼,系統將在執行它們時暫停:
static class Feature {
Feature ( String description ) {
this . description = description ;
}
String description () throws InterruptedException {
Thread . sleep ( 1000 );
return description ;
}
}
如果您變更描述以傳回Callable
,如果您在建立MustacheFactory
時提供了ExecutorService
它將自動在單獨的執行緒中執行。
Callable < String > description () throws InterruptedException {
return new Callable < String >() {
@ Override
public String call () throws Exception {
Thread . sleep ( 1000 );
return description ;
}
};
}
這支援計劃任務、流行為和非同步 I/O。查看example
模組以查看完整的端到端範例:
package mustachejava ;
import com . github . mustachejava . DefaultMustacheFactory ;
import com . github . mustachejava . Mustache ;
import com . github . mustachejava . MustacheFactory ;
import java . io . IOException ;
import java . io . PrintWriter ;
import java . io . Writer ;
import java . util . Arrays ;
import java . util . List ;
public class Example {
List < Item > items () {
return Arrays . asList (
new Item ( "Item 1" , "$19.99" , Arrays . asList ( new Feature ( "New!" ), new Feature ( "Awesome!" ))),
new Item ( "Item 2" , "$29.99" , Arrays . asList ( new Feature ( "Old." ), new Feature ( "Ugly." )))
);
}
static class Item {
Item ( String name , String price , List < Feature > features ) {
this . name = name ;
this . price = price ;
this . features = features ;
}
String name , price ;
List < Feature > features ;
}
static class Feature {
Feature ( String description ) {
this . description = description ;
}
String description ;
}
public static void main ( String [] args ) throws IOException {
MustacheFactory mf = new DefaultMustacheFactory ();
Mustache mustache = mf . compile ( "template.mustache" );
mustache . execute ( new PrintWriter ( System . out ), new Example ()). flush ();
}
}
提供變數的另一種方法是使用 Map 對象,例如:
public static void main ( String [] args ) throws IOException {
HashMap < String , Object > scopes = new HashMap < String , Object >();
scopes . put ( "name" , "Mustache" );
scopes . put ( "feature" , new Feature ( "Perfect!" ));
Writer writer = new OutputStreamWriter ( System . out );
MustacheFactory mf = new DefaultMustacheFactory ();
Mustache mustache = mf . compile ( new StringReader ( "{{name}}, {{feature.description}}!" ), "example" );
mustache . execute ( writer , scopes );
writer . flush ();
}