访问 localhost:3001/test 会得到以下 HTML:
<html>
<head>
</head>
<body>clojure.lang.LazySeq@27237276</body>
</html>
Clojure 代码:
(ns notebook.handler
(:require [compojure.core :refer :all]
[compojure.handler :as handler]
[compojure.route :as route]
[net.cgrand.enlive-html :as html]))
(html/defsnippet welcome
(html/html [:h1]) ; html snippet
[:h1] ; selector
[username] ; arguments
[:h1] (html/content username)) ; substitution
(html/deftemplate home-page "templates/base.html"
[username]
[:body] (html/html-content (welcome username)))
(defroutes app-routes
(GET "/test" [] (home-page "oru"))
(route/resources "/")
(route/not-found "Not Found"))
(def app
(handler/site app-routes))
看起来我没有正确使用模板和/或在某处搞砸了懒惰。我试过放在doall
几个地方希望它能解决懒惰但没有骰子。
调试尝试:
(welcome "oru")
=> ({:tag :h1, :attrs {}, :content ("oru")})
(html/emit* (welcome "oru"))
=> ("<" "h1" ">" "oru" "</" "h1" ">")
到目前为止,一切都很好...
(home-page "oru")
=> ("<" "html" ">" "\n " "<" "head" ">" "\n " "</" "head" ">" "\n " "<" "body" ">" "clojure.lang.LazySeq@27237276" "</" "body" ">" "\n\n" "</" "html" ">")
砰!"clojure.lang.LazySeq@27237276"
,这到底是在做什么?