Simple Object-oriented Mustache rendering engine

Mustache rendering engine which follows Object-oriented practices.

License

License

GroupId

GroupId

com.github.piotrkot
ArtifactId

ArtifactId

mustache
Last Version

Last Version

1.2
Release Date

Release Date

Type

Type

jar
Description

Description

Simple Object-oriented Mustache rendering engine
Mustache rendering engine which follows Object-oriented practices.
Project URL

Project URL

https://github.com/piotrkot/simple-mustache
Source Code Management

Source Code Management

https://github.com/piotrkot/simple-mustache

Download mustache

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.piotrkot/mustache/ -->
<dependency>
    <groupId>com.github.piotrkot</groupId>
    <artifactId>mustache</artifactId>
    <version>1.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.piotrkot/mustache/
implementation 'com.github.piotrkot:mustache:1.2'
// https://jarcasting.com/artifacts/com.github.piotrkot/mustache/
implementation ("com.github.piotrkot:mustache:1.2")
'com.github.piotrkot:mustache:jar:1.2'
<dependency org="com.github.piotrkot" name="mustache" rev="1.2">
  <artifact name="mustache" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.piotrkot', module='mustache', version='1.2')
)
libraryDependencies += "com.github.piotrkot" % "mustache" % "1.2"
[com.github.piotrkot/mustache "1.2"]

Dependencies

compile (5)

Group / Artifact Type Version
com.google.guava : guava jar 18.0
org.cactoos : cactoos jar 0.42
org.slf4j : slf4j-api jar 1.7.18
org.slf4j : slf4j-simple jar 1.7.18
org.hamcrest : hamcrest-all jar 1.3

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.6

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Simple-mustache

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.

Tag types supported

Variables

The basic tag {{name}} Example:

new Mustache("Hello {{name}}").supply(ImmutableMap.of("name", "John"));

returns Hello John.

Sections

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.

Inverted sections

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.

Partials

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!.

Delimiters

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 "]]";
    }
}

Limitations

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.2</version>
</dependency>

Feel free to fork me on GitHub, report bugs or post comments.

For Pull Requests, please run mvn clean package -Pqulice, first.

Versions

Version
1.2
1.1
1.0