This is simple object-oriented Java implementation of mustache templates. Although there is already available one from spullara I personally find it difficult to extend and fix, with unclear API, and few bugs when different tag delimiter used.
For that I propose a simpler and object-oriented alternative.
The basic tag {{name}}
Example:
new Mustache("Hello {{name}}").supply(ImmutableMap.of("name", "John"));
returns Hello John
.
Tag composed of two tags {{#name}} {{/name}}
for which content within the tags
is rendered one or more times.
Example:
new Mustache("Greetings to{{#friends}} {{name}}{{/friends}}").supply(
ImmutableMap.of(
"friends", ImmutableList.of(
ImmutableMap.of("name", "John"),
ImmutableMap.of("name", "Mark"),
ImmutableMap.of("name", "Ann")
)
)
);
returns Greetings to John Mark Ann
.
Tag composed of two tags {{^name}} {{/name}}
which is the inversion of
the section tag. That is content is rendered only once on the inverse value
of the tag name (tag key).
Example:
new Mustache("Greetings in return{{^greets}} - none{{/greets}}").supply(
ImmutableMap.of("greets", Collections.emptyList())
);
returns Greetings in return - none
.
It's of a form {{>name}}
where value for the tag name is file content.
File content can be a String, Path or InputStream. The content can contain
other Tag types except partials. Recursive partials are not supported to avoid
infinite loops.
Example:
new Mustache("Hope to get a {{>surprise}}").supply(
ImmutableMap.of("surprise", new ByteArrayInputStream("gift!".getBytes()))
);
returns Hope to get a gift!
.
Arbitrarily any delimiter can be used but use it with care. Otherwise you will end up with unexpected outcome. Example:
public final class SquareMustache extends AbstractMustache {
public SquareMustache(final String content) {
super(content);
}
@Override
public String start() {
return "[[";
}
@Override
public String end() {
return "]]";
}
}
There is no validation phase for content generation. Illegal template or tags will be unrecognized without a warning.
Tag names must be composed of any word characters (A-Za-z0-9_
)
including dot (.
).
Within tags spaces are allowed as long as they do not split the tag names.
To get started, add dependency to your project:
<dependency>
<groupId>com.github.piotrkot</groupId>
<artifactId>mustache</artifactId>
<version>1.3.2</version>
</dependency>
Feel free to fork me on GitHub, report bugs or post comments.
For Pull Requests, please run mvn clean package
, first.