【问题标题】:Enlive snippet in template produces lazy sequence模板中的 Enlive 片段产生惰性序列
【发布时间】:2014-01-22 04:13:15
【问题描述】:

访问 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",这是在搞什么鬼?

【问题讨论】:

    标签: clojure enlive


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-12-02
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2013-01-04
      • 2020-03-06
      • 2011-06-13
      • 2023-03-09
      • 2014-06-23
      相关资源
      最近更新 更多