假设我想要编写一个Clojure函数,该函数返回一个与<h3>Hello</h3>等价的Hiccup结构。
,我该怎么做?
我试过了
(defn render-location-details
[cur-location]
(let []
(list :h3 "Hello")
)
)和
(defn render-location-details
[cur-location]
(let []
[:h3 "Hello"]
)
)但是在这两种情况下都得到了错误消息(:h3 "Location X") is not a valid element name.。
更新1: --我从这里调用上面的函数:
(defn generate-location-details-report
[all-locations]
(let
[
hiccup-title [:h2 "Locations"]
hiccup-body (into []
(map
render-location-details
all-locations)
)
]
(str
(html hiccup-title)
hiccup-body
)
)
)有一个集合all-locations。对于它的每个元素,我希望在HTML中创建一个节(带有h3)头(hiccup-body),在标题(hiccup-title)前面加上一个部分,并将所有这些转换成HTML。
发布于 2021-06-01 09:27:45
html函数将接受一系列标记,并将它们呈现为字符串。
(let [locations ["one" "two" "three"]
title-html [[:h2 "Locations"]]
location-html (map (fn [location] [:h3 location]) locations)]
(html (concat title-html location-html)))
"<h2>Locations</h2><h3>one</h3><h3>two</h3><h3>three</h3>"第一个render-location-details无法工作,因为列表不是向量,因此Hiccup不会将其呈现为标记。
第二个render-location-details可以做您想做的事情。空的(let []绑定是不必要的。然而,Hiccup被放置hiccup-body (into []而混淆了--它试图将位置标记的向量理解为标记,因为就Hiccup而言,Hiccup= tag。
https://stackoverflow.com/questions/67779831
复制相似问题