Clojure:enlive deftemplate can';不要使用代码片段
我正在尝试创建一个模板来生成包含一些数据的表。数据来自msh内容中定义的映射Clojure:enlive deftemplate can';不要使用代码片段,clojure,enlive,Clojure,Enlive,我正在尝试创建一个模板来生成包含一些数据的表。数据来自msh内容中定义的映射 (require '[net.cgrand.enlive-html :as html]) (def msh-contents {:title "Events mashup", :data-content [{:title "ICTM Study Group ", :url "http://some-ur
(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传递给PersistentArrayMap clojure.lang.AFn.throwArity(AFn.java:437)的参数数目错误(0),这可能是因为代码段产生LazySeq。这可能非常愚蠢,但代码对我来说运行得非常好。我所做的唯一区别是将html表示为hiccup符号,然后转换为节点,这相当于给它一个html文件(我这样做是因为我还没有弄清楚
html资源
是如何工作的,也不确定我可以将文件放在哪里)。工作代码如下:
(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>"
(clojure.string/join(mshp msh内容))
=>“这是一个表格titleurlICTM研究组http://some-url.comVolodja 巴尔扎洛斯基-兴科Haashttp://some- “其他url.com”
在执行
(模板div)
时,您可能给defsnippet
提供了一个错误的源代码defsnippet
和deftemplate
都使用节点。当您给他们一个类似于“index.html”
的文件时,它会将该文件转换为节点 谢谢,我会调查的。我不确定defsnippet是否正常工作,因为我自己尝试过,结果很好。我必须用它尝试一些东西,所以它不会返回懒惰的seq,而是字符串。太好了,我喜欢打嗝。这是html资源的问题,因此hiccup的解决方案要好得多。