在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:spullara/mustache.java开源软件地址:https://github.com/spullara/mustache.java开源编程语言:HTML 63.4%开源软件介绍:Mustache.javaMustache.java is not designed to allow untrusted parties to provide templates. It may be possible to lock it down to provide that safely, but by default it is UNSAFE. Use the SafeMustacheFactory and whitelist all templates and partials. As of release 0.9.0 mustache.java is now Java 8 only. For Java 6/7 support use 0.8.x. There are no external dependencies and the compiler library is ~100k. Mustache.java is a derivative of mustache.js. There is a Google Group for support and questions: http://groups.google.com/group/mustachejava Travis CI: https://travis-ci.org/spullara/mustache.java API documentation: http://spullara.github.io/mustache/apidocs/ Largest production deployment of Mustache.java:
Thanks to YourKit for many performance improvements: YourKit is kindly supporting the mustache.java open source project with its full-featured Java Profiler. YourKit, LLC is the creator of innovative and intelligent tools for profiling Java and .NET applications. Take a look at YourKit's leading software products: Request for contributions:
Documentation:
Performance:
Build suggestions:
Maven dependency information (ie. for most common cases you will just need the 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> Example template file:
Might be powered by some backing code: 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;
}
} And would result in:
Evaluation of the template proceeds serially. For instance, if you have blocking code within one of your callbacks, the system will pause while executing them: static class Feature {
Feature(String description) {
this.description = description;
}
String description() throws InterruptedException {
Thread.sleep(1000);
return description;
}
} If you change description to return a Callable<String> description() throws InterruptedException {
return new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(1000);
return description;
}
};
} This enables scheduled tasks, streaming behavior and asynchronous i/o. Check out the 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();
}
} An alternative approach for providing variables would be to use a Map object, like: 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();
} License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论