Created
December 19, 2018 02:19
-
-
Save lordk911/3311a4564ae679d800d9cc2006bf323e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.github.mustachejava.DefaultMustacheFactory; | |
import com.github.mustachejava.Mustache; | |
import com.github.mustachejava.MustacheFactory; | |
import com.github.mustachejava.reflect.MissingWrapper; | |
import com.github.mustachejava.reflect.ReflectionObjectHandler; | |
import com.github.mustachejava.util.Wrapper; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.io.StringReader; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class MustacheExample { | |
public List<Item> items() { | |
return Arrays.asList(new Item("Item 1", "$19.99", Arrays.asList(new Feature("New!"), new Feature("Awesome!"))), | |
new Item("Item 2", "$29.99", Arrays.asList(new Feature("Old."), new Feature("Ugly.")))); | |
} | |
public static void main(String[] args) throws IOException { | |
MustacheExample example = new MustacheExample(); | |
MustacheFactory mf = example.getMustacheFactory(); // new DefaultMustacheFactory(); | |
// Mustache mustache = mf.compile("template.mustache"); | |
// mustache.execute(new PrintWriter(System.out), new MustacheExample()).flush(); | |
// String templete = "{{#items}}\r\n" + | |
// " Name: {{name}}\r\n" + | |
// " Price: {{price}}\r\n" + | |
// " {{#features}}\r\n" + | |
// " Feature: {{description}}\r\n" + | |
// " {{/features}}\r\n" + | |
// "{{/items}}"; | |
// | |
// Mustache mustache2 = mf.compile(new StringReader(templete), "any"); | |
// mustache2.execute(new PrintWriter(System.out), new | |
// MustacheExample()).flush();' | |
String templete2 = "Hello {{name|mustache.java}} \n" + "You have just won ${{value}}! \n" + "{{#in_ca}} " | |
+ "Well, ${{taxed_value}}, after taxes. \n" + "{{/in_ca}} "; | |
Map<String, Object> ctx = new HashMap<String, Object>(); | |
// ctx.put("name", "Chris"); | |
ctx.put("value", "10000"); | |
ctx.put("taxed_value", 10000 - (10000 * 0.4)); | |
ctx.put("in_ca", true); | |
Mustache mustache3 = mf.compile(new StringReader(templete2), "any"); | |
mustache3.execute(new PrintWriter(System.out), ctx).flush(); | |
} | |
public MustacheFactory getMustacheFactory() { | |
DefaultMustacheFactory mf = new DefaultMustacheFactory(); | |
mf.setObjectHandler(new ReflectionObjectHandler() { | |
@Override | |
public Wrapper find(String name, List<Object> scopes) { | |
int i; | |
if ((i = name.indexOf("|")) != -1) { | |
String newName = name.substring(0, i); | |
String defaultValue = name.substring(i + 1); | |
Wrapper wrapper = super.find(newName, scopes); | |
if (wrapper instanceof MissingWrapper) { | |
return scopes1 -> { | |
// Test the guards returned in the missing wrapper | |
//System.out.println("missing -> " + wrapper.call(scopes1)); | |
return defaultValue; | |
}; | |
} | |
return wrapper; | |
} | |
return super.find(name, scopes); | |
} | |
}); | |
return mf; | |
} | |
static class Feature { | |
private String description; | |
public Feature(String description) { | |
this.description = description; | |
} | |
public String getDescription() { | |
return description; | |
} | |
public void setDescription(String description) { | |
this.description = description; | |
} | |
} | |
public static class Item { | |
private String name, price; | |
private List<Feature> features; | |
public Item(String name, String price, List<Feature> features) { | |
this.name = name; | |
this.price = price; | |
this.features = features; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getPrice() { | |
return price; | |
} | |
public void setPrice(String price) { | |
this.price = price; | |
} | |
public List<Feature> getFeatures() { | |
return features; | |
} | |
public void setFeatures(List<Feature> features) { | |
this.features = features; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment