JMustachemustache 的 Java 實現(xiàn)
jmustache 是 Mustache 模板引擎的java實現(xiàn)。
-
零依賴性。可以在項目中包含該單個微型庫,然后開始使用模板。
-
在各種目標(biāo)平臺上的可用性。另一種Java Mustache實現(xiàn)要求需要Java編譯器才能將模板編譯為Java類。此實現(xiàn)沒有任何此類要求,因此可在Android或其他無法使用Java編譯器的令人興奮的地方使用。如果需要,甚至可以避免使用反射,并將所有數(shù)據(jù)作為一系列嵌套Maps提供。
-
Proguard和JarJar 友好。盡管該庫將以反射方式訪問您的數(shù)據(jù)(如果需要),但該庫在反射或類的名稱實例化方面沒有其他內(nèi)部用途。因此,您可以使用Proguard或JarJar嵌入它,而不會出現(xiàn)任何令人討厭的驚喜。
-
最小的API占用空間。您實際上只需要知道兩種方法:
compile和execute。在性能無關(guān)緊要的情況下,您甚至可以將它們鏈接在一起。 - 可通過Maven Central使用,詳情請參見下文。
- 它表現(xiàn)合理。模板與執(zhí)行分開解析。模板會將其變量專門用于(上下文的類,名稱)對,因此,如果首先將變量解析為(例如)上下文對象的字段,則將在隨后的模板調(diào)用時直接嘗試使用該變量,而較慢的完全調(diào)用僅當(dāng)字段訪問變量失敗時,才嘗試解析。
使用方法:
String text = "One, two, {{three}}. Three sir!";
Template tmpl = Mustache.compiler().compile(text);
Map<String, String> data = new HashMap<String, String>();
data.put("three", "five");
System.out.println(tmpl.execute(data));
// result: "One, two, five. Three sir!"
示例2:
class Person {
public final String name;
public Person (String name, int age) {
this.name = name;
_age = age;
}
public int getAge () {
return _age;
}
protected int _age;
}
String tmpl = "{{#persons}}{{name}}: {{age}}{{/persons}}\n";
Mustache.compiler().compile(tmpl).execute(new Object() {
Object persons = Arrays.asList(new Person("Elvis", 75), new Person("Madonna", 52));
});
// result:
// Elvis: 75
// Madonna: 52評論
圖片
表情
