2

访问 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",这到底是在做什么?

4

1 回答 1

2

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

于 2014-01-21T20:13:15.560 回答