Spring Boot With Mustache#
What Is The Mustache?#
Mustache
is a logic-less templates.Mustache
can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object.- 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.
- More information
Mustache Syntax#
- Let's check more detail in this page
Variables#
- The most basic tag type is the variable. A
{{name}}
tag in a basic template will try to find thename
key in the current context. If there is noname
key, the parent contexts will be checked recursively. If the top context is reached and thename
key is still not found, nothing will be rendered. - All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache:
{{{name}}}
. - You can also use
&
to unescape a variable:{{& name}}
. This may be useful when changing delimiters (see "Set Delimiter" below). - By default a variable "miss" returns an empty string. This can usually be configured in your Mustache library. The Ruby version of Mustache supports raising an exception in this situation, for instance.
- Template: sample.mustache
sample.mustache | |
---|---|
1 2 3 |
|
- Result
1 2 3 4 5 |
|
Section#
- Sections render blocks of text one or more times, depending on the value of the key in the current context.
- A section begins with a pound and ends with a slash. That is,
{{#persons}}
begins a "person" section while{{/persons}}
ends it. - The behavior of the section is determined by the value of the key.
- False Values or Empty Lists
- If the
persons
key exists and has a value of false or an empty list, the HTML between the pound and slash will not be displayed.
person.mustache | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
- Result:
1 2 3 4 5 6 7 8 9 10 |
|
Inverted Sections#
- An inverted section begins with a caret (hat) and ends with a slash. That is
{{^person}}
begins a "person" inverted section while{{/person}}
ends it. - While sections can be used to render text one or more times based on the value of the key, inverted sections may render text once based on the inverse value of the key. That is, they will be rendered if the key doesn't exist, is false, or is an empty list.
1 2 3 4 5 6 7 8 9 10 11 |
|
- Results: that car has values
1 2 3 4 5 6 |
|
- Results: that car has no values
1 2 3 4 5 |
|
Comments#
- Comments begin with a bang and are ignored. The following template:
1 2 3 4 5 |
|
- Will render as follows:
1 2 3 4 5 |
|
Partials#
- Partials begin with a greater than sign, like
{{> laptop}}
. - Partials are rendered at runtime (as opposed to compile time), so recursive partials are possible. Just avoid infinite loops.
- Mustache requires only this:
{{> laptop}}
- Why? Because the
person.mustache
file will inherit thesize
andstart
methods from the calling context. - In this way you may want to think of partials as includes, imports, template expansion, nested templates, or sub-templates, even though those aren't literally the case here.
- For example, this template and partial:
person.mustache | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
- Can be thought of as a single, expanded template:
laptop.mustache | |
---|---|
1 2 3 4 5 |
|
Mustache Example With Spring Boot#
- Now, let's take an example with using mustache in spring boot service for mapping json.
Dependencies#
- Let's add the dependency below for using mustache in spring boot.
pom.xml | |
---|---|
1 2 3 4 5 |
|
Create Mustache Templates#
- Now, in the
resources
in your spring boot project, we will create a directory with namedtemplates
. This directory will be a place to contain all your mustache templates. - Then let's create 2 templates,
person.mustache
andlaptop.mustache
as below
person.mustache | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
laptop.mustache | |
---|---|
1 2 3 4 5 |
|
- As you can see, we will bind data to the template
person
andlaptop
in which the templatelaptop
will be a sub-template of templateperson
. In this example we will use the json data as below to map key-value with templates above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Controller#
- Let's create a simple controller with api as below, then we can use postman to test.
MustacheController.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Service#
- Let's create a service class
MustacheTemplateService
and put the code as below, to map the input json to the template and get the mapped json back.
MustacheTemplateService.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
- We will need to @Autowired two service from the Mustache library, they are
TemplateLoader
andCompiler
.- In which the
TemplateLoader
with methodgetTemplate()
is use to get the root mustache template that you created insources/templates/
. - The
Compiler
is used to configure binding data into the template, in this example, we will configure the mustachecompiler
ignore null values, so with non null and string values, they will be wrapped into the double quotes" "
for other values as null, number and boolean value, they will be keep as default.
- In which the
Testing#
- Now, let's start your spring boot service and use postman to execute the api with body as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
- Then you will received with response json as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|