Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
// Initialize the handlebars class | |
Handlebars handlebars = new Handlebars(); | |
// Compile the required template file | |
Template template = handlebars.compile("mytemplate"); | |
// Data | |
Map<String, String> data = new HashMap<>(); | |
data.put("name", "John"); |
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
TemplateLoader loader = new FileTemplateLoader("/home/templates/"); | |
Handlebars handlebars = new Handlebars(loader); | |
Template template = handlebars.compile("mytemplate"); | |
Map<String, String> data = new HashMap<>(); | |
data.put("name", "John"); | |
System.out.println(template.apply(data)); |
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
// Initialize the loader using a prefix | |
TemplateLoader loader = new HTTPTemplateLoader("https://bucket-name.s3.Region.amazonaws.com/"); | |
Handlebars handlebars = new Handlebars(loader); | |
Template template = handlebars.compile("index"); | |
Map<String, String> data = new HashMap<>(); | |
data.put("name", "John"); | |
System.out.println(template.apply(data)); |
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
(require '[hbs.core :as hbs]) | |
;; render template string | |
(hbs/render "Hello {{person.name}}!" {:person {:name "John"}}) | |
;; OR | |
;; render template file with a registry | |
(def reg (hbs/registry (hbs/classpath-loader "/templates"))) | |
(hbs/render-file reg "mytemplate" {:name "John"}) |
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
(defn http-loader | |
[base-url-prefix] | |
(doto (proxy | |
[URLTemplateLoader] [] | |
(^String resolve [^String path] | |
(str (proxy-super getPrefix) | |
path | |
(.getSuffix this))) |
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
;; Util to convert Clojure map to Java Hashmap | |
(defn clj-map->java-map | |
[data] | |
(postwalk | |
#(cond | |
(map? %) (java.util.HashMap. ^java.util.Map %) | |
(keyword? %) (name %) | |
:else %) | |
data)) |
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
public class HTTPTemplateLoader extends URLTemplateLoader { | |
public HTTPTemplateLoader(String baseUrl) { | |
setPrefix(normalize(baseUrl)); | |
} | |
public String resolve(final String filename) { | |
return getPrefix() + filename + getSuffix(); | |
} |