com.samskivert/jmustache
com.samskivert.jmustache
samskivert/jmustache
com.samskivert.jmustache
is an implementation of the Mustache templating language.
Mustache is one of many templating languages used for generating HTML. It has the unique advantage of being especially portable between languages and environments.
This comes as a result of a deliberate choice to not allow much "logic" in templates. To quote its man page:
We call it "logic-less" because there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values.
This comes at a cost - you need to arrange all the information for a template up-front - but does make it easier to consider the behavior of a template in isolation.
Of the implementations of mustache available for the JVM, com.samskivert.jmustache
has the fewest moving pieces, is up-to-date with the specification, and is faster than other implementations that do their work at runtime.
import com.samskivert.mustache.Mustache;
import java.util.List;
import java.util.Map;
Cartoon(String name, boolean hasMovie) {}
record
void main() {
= List.of(
var cartoons new Cartoon("Space Ghost Coast to Coast", false),
new Cartoon("Harvey Birdman, Attorney at Law", false),
new Cartoon("Sealab 2021", false),
new Cartoon("The Venture Bros.", true)
);
= """
var template <html>
<body>
<h1> Cartoons </h1>
<ul>
{{#cartoons}}
<li>
{{name}}
{{#hasMovie}}
(there is a movie)
{{/hasMovie}}
</li>
{{/cartoons}}
</ul>
</body>
</html>
""";
= Mustache.compiler()
var compiledTemplate .compile(template);
= compiledTemplate.execute(
var renderedTemplate Map.of("cartoons", cartoons)
);
// <html>
// <body>
// <h1> Cartoons </h1>
// <ul>
// <li>
// Space Ghost Coast to Coast
// </li>
// <li>
// Harvey Birdman, Attorney at Law
// </li>
// <li>
// Sealab 2021
// </li>
// <li>
// The Venture Bros.
// (there is a movie)
// </li>
// </ul>
// </body>
//</html>
System.out.println(renderedTemplate);
}