0

想象一下,我想编写一个 Clojure 函数,它返回一个等效于<h3>Hello</h3>.

我该怎么做?

我试过了

(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。

4

1 回答 1

1

打嗝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 而言,vector = tag。

于 2021-06-01T09:27:45.463 回答