【问题标题】:Clojure: why is sniptest producing LazySeqClojure:为什么 sniptest 会产生 LazySeq
【发布时间】:2014-09-03 12:37:48
【问题描述】:

我在 enlive 中创建了一个模板,但遇到了这个产生lazyseq 的 sn-p 问题。当我在 REPL 中尝试这个 sniptest 时,它会产生“clojure.lang.LazySeq@ba6da9f2”。

    (h/sniptest (template-div) 
     [:div.Row]  (h/content (map #(value-cell %) 
     (for [e(:data-content msh-contents)] 
      (vals e)))))

测试所需的其余代码如下所示

    (require '[net.cgrand.enlive-html :as h])

    (def msh-contents {:title "Events mashup",
                      :data-content [{:title "ICTM Study Group ",  
                                    :url "http://some-url.com"} 
                                    {:title "Volodja Balzalorsky - Hinko Haas",
                                    :url "http://some- other-url.com"}
                                    ]})

    (defn template-div[] (h/html-resource "index.html"))

    (h/defsnippet value-cell (template-div) 
                             [:div.Row :div.Cell] [value]
                             (h/content value))

index.html 文件看起来像这样(也可以在这里找到http://www.filedropper.com/index_25))

    <div class="Table">
    <div class="Title">
    <p>This is a Table</p>
    </div>
    <div class="Heading">
    <div class="Cell">
        <p>Heading 1</p>
    </div>
    </div>
    <div class="Row">
    <div class="Cell">
        <p>Row 1 Column 1</p>
   </div>
   </div>

我看到了一个类似的问题,但解决方案是使用内容而不是 html-content。不知道是什么导致了这里的问题......

【问题讨论】:

  • 不恰当的缩进真的很令人沮丧。
  • @A.Webb 我改变了缩进,抱歉。

标签: clojure lazy-sequences enlive


【解决方案1】:

来自https://github.com/cgrand/enlive/wiki/Getting-started的示例

x=> (sniptest "<html><body><span>Hello </span>" 
      [:span] (append "World"))
"<html><body><span>Hello World</span></body></html>"

来自html-resource 文档字符串:"Loads an HTML resource, returns a seq of nodes."

请注意示例中的源代码是 html 字符串的形式,而不是 seq of nodes。为什么它的工作方式击败了我,但您可能想要以下内容:

(h/sniptest (clojure.string/join (h/emit* (template-div))) ; this feeds it a html str instead 
 [:div.Row]  (h/content (map #(value-cell %) 
 (for [e(:data-content msh-contents)] 
  (vals e)))))

PS:你使用 sniptest 是为了什么,因为我直到现在才知道它的存在。然后我再次以一种奇怪的方式使用 enlive(没有 deftemplates 或 defsn-ps,使用打嗝式 html,并且大量使用宏)。

【讨论】:

  • 我用它来尝试sn-p,因为我的deftemplate不起作用,因为sn-p产生了LazySeq。这确实有效,但我的 deftemplate 仍然没有:(
  • 如果你有时间可以在这里查看问题stackoverflow.com/questions/24727208/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 2020-08-05
  • 2012-08-08
  • 1970-01-01
  • 1970-01-01
  • 2017-08-08
相关资源
最近更新 更多