模板中的Enlive片段会产生延迟序列

时间:2014-01-21 18:45:40

标签: clojure enlive

访问localhost:3001 / test得到以下HTML:

<html>
  <head>
  </head>
  <body>clojure.lang.LazySeq@27237276</body>

</html>

Clojure Code:

(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" ">")

的Bam! "clojure.lang.LazySeq@27237276",这是在做什么?

1 个答案:

答案 0 :(得分:2)

您希望使用content,而不是html-content,因为代码段会产生一系列节点。 html-content需要一串文字HTML内容,并且可能只是在其参数上调用str(在这种情况下是作为代码段输出的惰性序列)。