我有一堆描述位置的地图:
(def pizzeria
{
:LocationId "pizzeria"
:Desc "Pizzeria where Timur works"
:Address "Vorgartenstraße 126, 1020 Wien"
:Comment ""
})
(def dancing-school
{
:LocationId "dancing-school"
:Desc "Dancing school"
:Comment "4th district"
})
有些地图有:Comment
s,有些则没有。
我想创建一个 Clojure 函数,除其他外,将注释输出到 HTML(如果存在)。
我写了这个函数:
(defn render-location-details
[cur-location]
(let [
desc (get cur-location :Desc)
address (get cur-location :Address)
comment (get cur-location :Comment)
address-title [:h4 "Address"]
address-body [:p address]
comment-hiccup [
(if (clojure.string/blank? comment)
nil
[:div
[:h4 "Comment"]
[:p comment]
])
]
]
[:h3 desc
address-title
address-body
comment-hiccup
]
)
)
如果我运行使用此函数的代码,我会收到错误
Execution error (IllegalArgumentException) at
hiccup.compiler/normalize-element (compiler.clj:59).
is not a valid element name.
如果我更改comment-hiccup
为nil
,则错误消失。
如何使用 Hiccup 有条件地将数据输出到 HTML?
注意:我是 Clojure 的新手,所以如果我的方法完全错误,请告诉并展示如何正确操作。