Last active
August 29, 2015 13:56
-
-
Save Centaur/8892957 to your computer and use it in GitHub Desktop.
use httl in clojure
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
(ns clojure-scripts.template | |
(:import (httl Engine))) | |
(def model {:single_string "Hello, world!" | |
:items [{:name "item1"} | |
{:name "item2"} | |
{:name "item3"}]}) | |
(defn keyword-as-str [model] | |
(cond | |
(map? model) (into {} (for [[k v] model] | |
(cond | |
(keyword? k) [(name k) (keyword-as-str v)] | |
:else [k (keyword-as-str v)]))) | |
(sequential? model) (map keyword-as-str model) | |
:else model)) | |
(def engine (Engine/getEngine)) | |
(def tplt (.getTemplate engine "test.httl")) | |
(.render tplt (keyword-as-str model) *out*) |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
${ single_string } | |
<ul> | |
<!--#for(Map item: items)--> | |
<li>${item.name}</li> | |
<!--#end--> | |
</ul> | |
</body> | |
</html> |
Wow, clojure.walk is a powerful tool. Thanks a lot.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:) (clojure.walk/postwalk #(if(keyword? %)(name %) %) model) can do your job