Moustache.java tidak dirancang untuk mengizinkan pihak yang tidak dipercaya menyediakan template. Dimungkinkan untuk menguncinya untuk menyediakannya dengan aman, tetapi secara default itu TIDAK AMAN. Gunakan SafeMustacheFactory dan masukkan semua templat dan sebagian ke daftar putih.
Pada rilis 0.9.0 mustache.java sekarang hanya Java 8. Untuk dukungan Java 6/7 gunakan 0.8.x.
Tidak ada ketergantungan eksternal dan perpustakaan kompiler ~100k.
Moustache.java merupakan turunan dari mustache.js.
Ada Grup Google untuk dukungan dan pertanyaan: http://groups.google.com/group/mustachejava
Github CI: https://github.com/spullara/mustache.java/actions/workflows/maven.yml
Dokumentasi API: http://spullara.github.io/mustache/apidocs/
Penerapan produksi terbesar dari Moustache.java:
Terima kasih kepada YourKit untuk banyak peningkatan kinerja:
YourKit dengan baik hati mendukung proyek sumber terbuka mustache.java dengan Java Profiler berfitur lengkap. YourKit, LLC adalah pencipta alat inovatif dan cerdas untuk membuat profil aplikasi Java dan .NET. Lihatlah produk perangkat lunak terkemuka YourKit:
Permintaan kontribusi:
Dokumentasi:
mustache
perbedaan spasi moduloIterable
apa pun dapat digunakan untuk perilaku seperti daftarCallable
memungkinkan evaluasi bersamaan jika ExecutorService
dikonfigurasi{{<super}}{{$content}}...{{/content}}{{/super}}
){{#func1}}...{{/func1}}
) diimplementasikan menggunakan Function
dari Java 8 (pasca substitusi)TemplateFunction
jika Anda ingin mustache.java mengurai ulang hasil fungsi/lambda Anda (pra-substitusi)handlebar
akan merender templat + data json untuk maket cepat templat oleh desainerCapturingMustacheVisitor
untuk tiruan dan pengujianinvert
dapat mengambil teks dan template dan memecahkan datanyaPertunjukan:
com.github.mustachejavabenchmarks
di modul compiler
indy
menggunakan modul codegen dan invokedynamic untuk mengkompilasi template hingga bytecodeBuat saran:
Informasi ketergantungan Maven (mis. untuk sebagian besar kasus umum, Anda hanya memerlukan modul compiler
):
Jawa 8+:
< dependency >
< groupId >com.github.spullara.mustache.java</ groupId >
< artifactId >compiler</ artifactId >
< version >0.9.10</ version >
</ dependency >
Jawa 6/7:
< dependency >
< groupId >com.github.spullara.mustache.java</ groupId >
< artifactId >compiler</ artifactId >
< version >0.8.18</ version >
</ dependency >
Contoh file templat:
{{#items}}
Name: {{name}}
Price: {{price}}
{{#features}}
Feature: {{description}}
{{/features}}
{{/items}}
Mungkin didukung oleh beberapa kode pendukung:
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 ;
}
}
Dan akan menghasilkan:
Name: Item 1
Price: $19.99
Feature: New!
Feature: Awesome!
Name: Item 2
Price: $29.99
Feature: Old.
Feature: Ugly.
Evaluasi template berlangsung secara serial. Misalnya, jika Anda memiliki kode pemblokiran dalam salah satu panggilan balik Anda, sistem akan berhenti sejenak saat menjalankannya:
static class Feature {
Feature ( String description ) {
this . description = description ;
}
String description () throws InterruptedException {
Thread . sleep ( 1000 );
return description ;
}
}
Jika Anda mengubah deskripsi untuk mengembalikan Callable
, maka secara otomatis akan dieksekusi di thread terpisah jika Anda telah menyediakan ExecutorService
saat membuat MustacheFactory
Anda.
Callable < String > description () throws InterruptedException {
return new Callable < String >() {
@ Override
public String call () throws Exception {
Thread . sleep ( 1000 );
return description ;
}
};
}
Hal ini mengaktifkan tugas terjadwal, perilaku streaming, dan i/o asinkron. Lihat example
modul untuk melihat contoh end-to-end yang lengkap:
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 ();
}
}
Pendekatan alternatif untuk menyediakan variabel adalah dengan menggunakan objek Map, seperti:
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 ();
}