2014-01-21 65 views
2

訪問本地主機:3001 /測試結果在下面的HTML:Enlive片斷產生懶惰序列

<html> 
    <head> 
    </head> 
    <body>[email protected]</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" ">" "[email protected]" "</" "body" ">" "\n\n" "</" "html" ">") 

巴姆! "[email protected]",這是幹什麼的?

回答

2

您想使用content而不是html-content,因爲片段會生成一系列節點。 html-content需要一串字面的HTML內容,並且可能只是在其參數上調用str(在這種情況下,是您的片段輸出的惰性序列)。