BAEL-2981: Added code samples for Handlebars.java
This commit is contained in:
parent
7ce538e8cf
commit
1de5fc5a0d
|
@ -81,13 +81,17 @@
|
|||
<version>3.14.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>edu.uci.ics</groupId>
|
||||
<artifactId>crawler4j</artifactId>
|
||||
<version>${crawler4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>edu.uci.ics</groupId>
|
||||
<artifactId>crawler4j</artifactId>
|
||||
<version>${crawler4j.version}</version>
|
||||
</dependency>
|
||||
<!-- HandleBars -->
|
||||
<dependency>
|
||||
<groupId>com.github.jknack</groupId>
|
||||
<artifactId>handlebars</artifactId>
|
||||
<version>4.1.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -0,0 +1,104 @@
|
|||
package com.baeldung.handlebars;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.github.jknack.handlebars.Handlebars;
|
||||
import com.github.jknack.handlebars.Template;
|
||||
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
|
||||
import com.github.jknack.handlebars.io.TemplateLoader;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BasicUsageUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenThereIsNoTemplateFile_ThenCompilesInline() throws IOException {
|
||||
Handlebars handlebars = new Handlebars();
|
||||
Template template = handlebars.compileInline("Hi {{this}}!");
|
||||
|
||||
String templateString = template.apply("Baeldung");
|
||||
|
||||
assertThat(templateString).isEqualTo("Hi Baeldung!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenParameterMapIsSupplied_thenDisplays() throws IOException {
|
||||
Handlebars handlebars = new Handlebars();
|
||||
Template template = handlebars.compileInline("Hi {{name}}!");
|
||||
|
||||
Map<String, String> parameterMap = new HashMap<>();
|
||||
parameterMap.put("name", "Baeldung");
|
||||
String templateString = template.apply(parameterMap);
|
||||
|
||||
assertThat(templateString).isEqualTo("Hi Baeldung!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenParameterObjectIsSupplied_ThenDisplays() throws IOException {
|
||||
Handlebars handlebars = new Handlebars();
|
||||
Template template = handlebars.compileInline("Hi {{name}}!");
|
||||
|
||||
Person person = new Person();
|
||||
person.setName("Baeldung");
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("Hi Baeldung!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMultipleParametersAreSupplied_ThenDisplays() throws IOException {
|
||||
Handlebars handlebars = new Handlebars();
|
||||
Template template = handlebars.compileInline("Hi {{name}}! This is {{topic}}.");
|
||||
|
||||
Map<String, String> parameterMap = new HashMap<>();
|
||||
parameterMap.put("name", "Baeldung");
|
||||
parameterMap.put("topic", "Handlebars");
|
||||
String templateString = template.apply(parameterMap);
|
||||
|
||||
assertThat(templateString).isEqualTo("Hi Baeldung! This is Handlebars.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoLoaderIsGiven_ThenSearchesClasspath() throws IOException {
|
||||
Handlebars handlebars = new Handlebars();
|
||||
Template template = handlebars.compile("greeting");
|
||||
|
||||
Person person = getPerson("Baeldung");
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("Hi Baeldung!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenClasspathTemplateLoaderIsGiven_ThenSearchesClasspathWithPrefixSuffix() throws IOException {
|
||||
TemplateLoader loader = new ClassPathTemplateLoader("/handlebars", ".html");
|
||||
Handlebars handlebars = new Handlebars(loader);
|
||||
Template template = handlebars.compile("greeting");
|
||||
|
||||
Person person = getPerson("Baeldung");
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("Hi Baeldung!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMultipleLoadersAreGiven_ThenSearchesSequentially() throws IOException {
|
||||
TemplateLoader firstLoader = new ClassPathTemplateLoader("/handlebars", ".html");
|
||||
TemplateLoader secondLoader = new ClassPathTemplateLoader("/templates", ".html");
|
||||
Handlebars handlebars = new Handlebars().with(firstLoader, secondLoader);
|
||||
Template template = handlebars.compile("greeting");
|
||||
|
||||
Person person = getPerson("Baeldung");
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("Hi Baeldung!");
|
||||
}
|
||||
|
||||
private Person getPerson(String name) {
|
||||
Person person = new Person();
|
||||
person.setName(name);
|
||||
return person;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
package com.baeldung.handlebars;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.github.jknack.handlebars.Handlebars;
|
||||
import com.github.jknack.handlebars.Template;
|
||||
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
|
||||
import com.github.jknack.handlebars.io.TemplateLoader;
|
||||
import java.io.IOException;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BuiltinHelperUnitTest {
|
||||
|
||||
private TemplateLoader templateLoader = new ClassPathTemplateLoader("/handlebars", ".html");
|
||||
|
||||
@Test
|
||||
public void whenUsedWith_ThenContextChanges() throws IOException {
|
||||
Handlebars handlebars = new Handlebars(templateLoader);
|
||||
Template template = handlebars.compile("with");
|
||||
|
||||
Person person = getPerson("Baeldung");
|
||||
person.getAddress().setStreet("World");
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("\n<h4>I live in World</h4>\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsedWithMustacheStyle_ThenContextChanges() throws IOException {
|
||||
Handlebars handlebars = new Handlebars(templateLoader);
|
||||
Template template = handlebars.compile("with_mustache");
|
||||
|
||||
Person person = getPerson("Baeldung");
|
||||
person.getAddress().setStreet("World");
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("\n<h4>I live in World</h4>\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsedEach_ThenIterates() throws IOException {
|
||||
Handlebars handlebars = new Handlebars(templateLoader);
|
||||
Template template = handlebars.compile("each");
|
||||
|
||||
Person person = getPerson("Baeldung");
|
||||
Person friend1 = getPerson("Java");
|
||||
Person friend2 = getPerson("Spring");
|
||||
person.getFriends().add(friend1);
|
||||
person.getFriends().add(friend2);
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("\n<span>Java is my friend.</span>\n"
|
||||
+ "\n<span>Spring is my friend.</span>\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsedEachMustacheStyle_ThenIterates() throws IOException {
|
||||
Handlebars handlebars = new Handlebars(templateLoader);
|
||||
Template template = handlebars.compile("each_mustache");
|
||||
|
||||
Person person = getPerson("Baeldung");
|
||||
Person friend1 = getPerson("Java");
|
||||
Person friend2 = getPerson("Spring");
|
||||
person.getFriends().add(friend1);
|
||||
person.getFriends().add(friend2);
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("\n<span>Java is my friend.</span>\n"
|
||||
+ "\n<span>Spring is my friend.</span>\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsedIf_ThenPutsCondition() throws IOException {
|
||||
Handlebars handlebars = new Handlebars(templateLoader);
|
||||
Template template = handlebars.compile("if");
|
||||
|
||||
Person person = getPerson("Baeldung");
|
||||
person.setBusy(true);
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("\n<h4>Baeldung is busy.</h4>\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsedIfMustacheStyle_ThenPutsCondition() throws IOException {
|
||||
Handlebars handlebars = new Handlebars(templateLoader);
|
||||
Template template = handlebars.compile("if_mustache");
|
||||
|
||||
Person person = getPerson("Baeldung");
|
||||
person.setBusy(true);
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("\n<h4>Baeldung is busy.</h4>\n");
|
||||
}
|
||||
|
||||
private Person getPerson(String name) {
|
||||
Person person = new Person();
|
||||
person.setName(name);
|
||||
return person;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.handlebars;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.github.jknack.handlebars.Handlebars;
|
||||
import com.github.jknack.handlebars.Helper;
|
||||
import com.github.jknack.handlebars.Options;
|
||||
import com.github.jknack.handlebars.Template;
|
||||
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
|
||||
import com.github.jknack.handlebars.io.TemplateLoader;
|
||||
import java.io.IOException;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CustomHelperUnitTest {
|
||||
|
||||
private TemplateLoader templateLoader = new ClassPathTemplateLoader("/handlebars", ".html");
|
||||
|
||||
@Test
|
||||
public void whenHelperIsCreated_ThenCanRegister() throws IOException {
|
||||
Handlebars handlebars = new Handlebars(templateLoader);
|
||||
handlebars.registerHelper("person", new Helper<Person>() {
|
||||
@Override
|
||||
public Object apply(Person context, Options options) throws IOException {
|
||||
String busyString = context.isBusy() ? "busy" : "available";
|
||||
return context.getName() + " - " + busyString;
|
||||
}
|
||||
});
|
||||
Template template = handlebars.compile("person");
|
||||
|
||||
Person person = getPerson("Baeldung");
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("Baeldung - busy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenHelperSourceIsCreated_ThenCanRegister() throws IOException {
|
||||
Handlebars handlebars = new Handlebars(templateLoader);
|
||||
handlebars.registerHelpers(new HelperSource());
|
||||
Template template = handlebars.compile("person");
|
||||
|
||||
Person person = getPerson("Baeldung");
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("Baeldung - busy");
|
||||
}
|
||||
|
||||
private Person getPerson(String name) {
|
||||
Person person = new Person();
|
||||
person.setName(name);
|
||||
person.setBusy(true);
|
||||
return person;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.handlebars;
|
||||
|
||||
public class HelperSource {
|
||||
|
||||
public String person(Person context) {
|
||||
String busyString = context.isBusy() ? "busy" : "available";
|
||||
return context.getName() + " - " + busyString;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.handlebars;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Person {
|
||||
|
||||
private String name;
|
||||
private boolean busy;
|
||||
private Address address = new Address();
|
||||
private List<Person> friends = new ArrayList<>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public boolean isBusy() {
|
||||
return busy;
|
||||
}
|
||||
|
||||
public void setBusy(boolean busy) {
|
||||
this.busy = busy;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public List<Person> getFriends() {
|
||||
return friends;
|
||||
}
|
||||
|
||||
public void setFriends(List<Person> friends) {
|
||||
this.friends = friends;
|
||||
}
|
||||
|
||||
public static class Address {
|
||||
|
||||
private String street;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.handlebars;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.github.jknack.handlebars.Handlebars;
|
||||
import com.github.jknack.handlebars.Template;
|
||||
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
|
||||
import com.github.jknack.handlebars.io.TemplateLoader;
|
||||
import java.io.IOException;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ReusingTemplatesUnitTest {
|
||||
|
||||
private TemplateLoader templateLoader = new ClassPathTemplateLoader("/handlebars", ".html");
|
||||
|
||||
@Test
|
||||
public void whenOtherTemplateIsReferenced_ThenCanReuse() throws IOException {
|
||||
Handlebars handlebars = new Handlebars(templateLoader);
|
||||
Template template = handlebars.compile("page");
|
||||
|
||||
Person person = new Person();
|
||||
person.setName("Baeldung");
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("<h4>Hi Baeldung!</h4>\n<p>This is the page Baeldung</p>");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBlockIsDefined_ThenCanOverrideWithPartial() throws IOException {
|
||||
Handlebars handlebars = new Handlebars(templateLoader);
|
||||
Template template = handlebars.compile("simplemessage");
|
||||
|
||||
Person person = new Person();
|
||||
person.setName("Baeldung");
|
||||
String templateString = template.apply(person);
|
||||
|
||||
assertThat(templateString).isEqualTo("\n<html>\n"
|
||||
+ "<body>\n"
|
||||
+ "\n This is the intro\n\n"
|
||||
+ "\n Hi there!\n\n"
|
||||
+ "</body>\n"
|
||||
+ "</html>");
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
Hi {{name}}!
|
|
@ -0,0 +1,3 @@
|
|||
{{#each friends}}
|
||||
<span>{{name}} is my friend.</span>
|
||||
{{/each}}
|
|
@ -0,0 +1,3 @@
|
|||
{{#each friends}}
|
||||
<span>{{name}} is my friend.</span>
|
||||
{{/each}}
|
|
@ -0,0 +1 @@
|
|||
Hi {{name}}!
|
|
@ -0,0 +1 @@
|
|||
<h4>Hi {{name}}!</h4>
|
|
@ -0,0 +1,5 @@
|
|||
{{#if busy}}
|
||||
<h4>{{name}} is busy.</h4>
|
||||
{{else}}
|
||||
<h4>{{name}} is not busy.</h4>
|
||||
{{/if}}
|
|
@ -0,0 +1,5 @@
|
|||
{{#if busy}}
|
||||
<h4>{{name}} is busy.</h4>
|
||||
{{^}}
|
||||
<h4>{{name}} is not busy.</h4>
|
||||
{{/if}}
|
|
@ -0,0 +1,9 @@
|
|||
<html>
|
||||
<body>
|
||||
{{#block "intro"}}
|
||||
This is the intro
|
||||
{{/block}}
|
||||
{{#block "message"}}
|
||||
{{/block}}
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,2 @@
|
|||
{{>header}}
|
||||
<p>This is the page {{name}}</p>
|
|
@ -0,0 +1 @@
|
|||
{{#person this}}{{/person}}
|
|
@ -0,0 +1,4 @@
|
|||
{{#partial "message" }}
|
||||
Hi there!
|
||||
{{/partial}}
|
||||
{{> messagebase}}
|
|
@ -0,0 +1,3 @@
|
|||
{{#with address}}
|
||||
<h4>I live in {{street}}</h4>
|
||||
{{/with}}
|
|
@ -0,0 +1,3 @@
|
|||
{{#address}}
|
||||
<h4>I live in {{street}}</h4>
|
||||
{{/address}}
|
Loading…
Reference in New Issue