Skip to content

Instantly share code, notes, and snippets.

@Centaur
Last active August 29, 2015 13:56
Show Gist options
  • Save Centaur/8892957 to your computer and use it in GitHub Desktop.
Save Centaur/8892957 to your computer and use it in GitHub Desktop.
use httl in clojure
(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*)
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
${ single_string }
<ul>
<!--#for(Map item: items)-->
<li>${item.name}</li>
<!--#end-->
</ul>
</body>
</html>
@weejulius
Copy link

:) (clojure.walk/postwalk #(if(keyword? %)(name %) %) model) can do your job

@Centaur
Copy link
Author

Centaur commented Feb 9, 2014

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