code for BAEL-860 - Introduction to mustache (#1885)

This commit is contained in:
Mohamed Sanaulla
2017-05-20 20:21:33 +03:00
committed by maibin
parent d1cc70666e
commit cfc44ecbb6
10 changed files with 299 additions and 0 deletions
@@ -0,0 +1,15 @@
package com.baeldung.mustache;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.MustacheFactory;
public class MustacheUtil {
private static final MustacheFactory mf = new DefaultMustacheFactory();
public static MustacheFactory getMustacheFactory(){
return mf;
}
}
@@ -0,0 +1,84 @@
package com.baeldung.mustache.model;
import java.time.Duration;
import java.time.temporal.Temporal;
import java.util.Date;
import java.util.function.Function;
public class Todo {
public Todo(){}
public Todo(String title, String text){
this.title = title;
this.text = text;
createdOn = new Date();
}
private String title;
private String text;
private boolean done = false;
private Date createdOn;
private Date completedOn;
public void markAsDone(){
done = true;
completedOn = new Date();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public boolean getDone() {
return done;
}
public Date getCreatedOn() {
return createdOn;
}
public Date getCompletedOn() {
return completedOn;
}
public void setDone(boolean done) {
this.done = done;
}
public void setCompletedOn(Date completedOn) {
this.completedOn = completedOn;
}
public long doneSince(){
if ( done ){
return Duration.between(createdOn.toInstant(), completedOn.toInstant()).toMinutes();
}
return 0;
}
public Function<Object, Object> handleDone(){
return (obj) -> {
if ( done ){
return String.format("<small>Done %s minutes ago<small>", obj);
}else{
return "";
}
};
}
}
@@ -0,0 +1,5 @@
{{#todo}}
<h2>{{title}}</h2>
<small>Created on {{createdOn}}</small>
<p>{{text}}</p>
{{/todo}}
@@ -0,0 +1,3 @@
<h2>{{title}}</h2>
<small>Created on {{createdOn}}</small>
<p>{{text}}</p>
@@ -0,0 +1,6 @@
{{#todos}}
<h2>{{title}}</h2>
{{/todos}}
{{^todos}}
<p>No todos!</p>
{{/todos}}
@@ -0,0 +1,3 @@
{{#todos}}
<h2>{{title}}{{#handleDone}}{{doneSince}}{{/handleDone}}</h2>
{{/todos}}
@@ -0,0 +1,3 @@
{{#todos}}
<h2>{{title}}</h2>
{{/todos}}