0

我正在尝试创建生成包含一些数据的表的模板。数据来自 msh-contents 中定义的映射。

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

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

   ;; define snippets based on some divs in the template
    (html/defsnippet header-cell (template-div) [:div.Heading :div.Cell][value]
    (html/content value))

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

   ;; define a template
   (html/deftemplate mshp "index.html" [cont]
   [:div.Heading] 
   (html/content (for [c (keys (first (:data-content cont)))] (header-cell (name c))))
   [:div.Row] 
   (html/content (map #(value-cell %) (for[e (:data-content cont)] (vals e)))))

所以,在 REPL 中调用模板

   (mshp msh-contents)

产生错误:IllegalArgumentException ArityException 传递给的 args (0) 数量错误:PersistentArrayMap clojure.lang.AFn.throwArity (AFn.java:437),这可能是因为片段产生了 LazySeq。

4

1 回答 1

1

这可能真的很愚蠢,但代码对我来说非常好。我所做的唯一区别是将您的 html 表示为打嗝符号,然后转换为节点,这相当于只给它一个 html 文件(我这样做是因为我还没有弄清楚究竟是如何html-resource工作的,也不确定我可以把文件放在哪里)。下面的工作代码:

(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 [:div {:class "Table"}
                [:div {:class "Title"}
                      [:p "This is a Table"]]
                [:div {:class "Heading"}
                      [:div {:class "Cell"}
                            [:p "Heading 1"]]]
                [:div {:class "Row"}
                      [:div {:class "Cell"}
                            [:p "Row 1 Column 1"]]]]))

(defn index []
  (h/html [:html [:div {:class "Table"}
                       [:div {:class "Title"}
                             [:p "This is a Table"]]
                       [:div {:class "Heading"}
                             [:div {:class "Cell"}
                                   [:p "Heading 1"]]]
                       [:div {:class "Row"}
                             [:div {:class "Cell"}
                                   [:p "Row 1 Column 1"]]]]]))

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

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

(h/deftemplate mshp (index) [cont]
               [:div.Heading] 
               (h/content (for [c (keys (first (:data-content cont)))] (header-cell (name c))))
               [:div.Row] 
               (h/content (map #(value-cell %) (for[e (:data-content cont)] (vals e)))))

最后结果:

(clojure.string/join (mshp msh-contents))
=> "<html><div class=\"Table\"><div class=\"Title\"><p>This is a Table</p></div><div class=\"Heading\"><div class=\"Cell\">title</div><div class=\"Cell\">url</div></div><div class=\"Row\"><div class=\"Cell\">ICTM Study Group http://some-url.com</div><div class=\"Cell\">Volodja Balzalorsky - Hinko Haashttp://some- other-url.com</div></div></div></html>"

也许你defsnippet在做(template-div). defsnippet并且deftemplate都取节点。当你给他们一个类似的文件"index.html"时,它会将文件转换为节点。

于 2014-07-13T22:35:07.800 回答