【发布时间】:2021-06-03 09:18:31
【问题描述】:
我有一堆描述位置的地图:
(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"
})
其中一些地图有:Comments,而另一些则没有。
我想创建一个 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 的新手,所以如果我的方法完全错误,请告诉并展示如何正确操作。
【问题讨论】: