Created
July 31, 2013 03:51
-
-
Save monzou/6119139 to your computer and use it in GitHub Desktop.
Handlebars.java
This file contains hidden or 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
<!-- RESULT --> | |
<!doctype html> | |
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> | |
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> | |
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> | |
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<title>application</title> | |
<meta name="description" content=""> | |
<meta name="viewport" content="width=device-width"> | |
<base href="/"> | |
<!-- Place favicon.ico and apple-touch-icon.png in the root directory --> | |
<link rel="stylesheet" href="styles/main.css"> | |
<!-- build:js scripts/vendor/modernizr.js --> | |
<script src="components/modernizr/modernizr.js"></script> | |
<!-- endbuild --> | |
<!-- build:js scripts/vendor/console-polyfill.js --> | |
<script src="components/console-polyfill/index.js"></script> | |
<!-- endbuild --> | |
</head> | |
<body> | |
<!--[if lt IE 7]> | |
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p> | |
<![endif]--> | |
<div id="application"> | |
</div> | |
<script type="text/javascript"> | |
var ns = window.App = window.App || {}; | |
ns.Options = { | |
accountingType: { | |
{ value: "T", displayName: "Trading" }, | |
{ value: "B", displayName: "<script type="text/javascript">Banking</script>" } | |
}, | |
productType: { | |
{ value: "IRS", displayName: "Interest Rate Swap" }, | |
{ value: "CRS", displayName: "Currency Swap" } | |
} | |
}; | |
</script> | |
<!-- build:js scripts/main.js --> | |
<script data-main="scripts/main" src="components/requirejs/require.js"></script> | |
<!-- endbuild --> | |
</body> | |
</html> | |
This file contains hidden or 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
package sandbox; | |
import java.io.IOException; | |
import java.util.LinkedList; | |
import java.util.List; | |
import com.github.jknack.handlebars.Handlebars; | |
import com.github.jknack.handlebars.Template; | |
import com.github.jknack.handlebars.io.ClassPathTemplateLoader; | |
public class Main { | |
public static void main(String[] args) { | |
ClassPathTemplateLoader loader = new ClassPathTemplateLoader(); | |
loader.setSuffix(".hbs"); | |
Handlebars handlebars = new Handlebars(loader); | |
try { | |
Template template = handlebars.compile("index"); | |
Model model = new Model(); | |
Model.OptionDto accountingType = model.addOption("accountingType"); | |
accountingType.addValue("T", "Trading"); | |
accountingType.addValue("B", "<script type=\"text/javascript\">Banking</script>"); // safe string | |
Model.OptionDto productType = model.addOption("productType"); | |
productType.addValue("IRS", "Interest Rate Swap"); | |
productType.addValue("CRS", "Currency Swap"); | |
String compiled = template.apply(model); | |
System.out.println(compiled); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private static class Model { | |
private String descriptor = "application"; | |
private String root = "/"; | |
private final LinkedList<OptionDto> options = new LinkedList<>(); | |
public String getDescriptor() { | |
return descriptor; | |
} | |
public String getRoot() { | |
return root; | |
} | |
public List<OptionDto> getOptions() { | |
return options; | |
} | |
OptionDto addOption(String name) { | |
OptionDto option = new OptionDto(); | |
option.setName(name); | |
options.add(option); | |
return option; | |
} | |
private class OptionDto { | |
private String name; | |
private final LinkedList<ValueDto> values = new LinkedList<>(); | |
public String getComma() { | |
return options.getLast() == this ? "" : ","; | |
} | |
public OptionDto addValue(String value, String displayName) { | |
ValueDto v = new ValueDto(); | |
v.setValue(value); | |
v.setDisplayName(displayName); | |
values.add(v); | |
return OptionDto.this; | |
} | |
public LinkedList<ValueDto> getValues() { | |
return values; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
private class ValueDto { | |
private String value; | |
private String displayName; | |
public String getComma() { | |
return values.getLast() == this ? "" : ","; | |
} | |
public String getValue() { | |
return value; | |
} | |
public void setValue(String value) { | |
this.value = value; | |
} | |
public String getDisplayName() { | |
return displayName; | |
} | |
public void setDisplayName(String displayName) { | |
this.displayName = displayName; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment