Clojure中的重复结构

Clojure中的重复结构,clojure,hiccup,Clojure,Hiccup,《使用Clojure进行Web开发》一书中说,代码 (defn registration-page [] (layout/common (form-to [:post "/register"] (label "id" "screen name") (text-field "id") [:br] (label "pass" "password") (pas

《使用Clojure进行Web开发》一书中说,代码

(defn registration-page []
    (layout/common
        (form-to [:post "/register"]
            (label "id" "screen name")
            (text-field "id")
            [:br]
            (label "pass" "password")
            (password-field "pass")
            [:br]
            (label "pass1" "retype password")
            (password-field "pass1")
            [:br]
            (submit-button "create account"))))
可以使用辅助函数重写,如下所示:

(defn control [field name text]
  (list (on-error name format-error)
        (label name text)
        (field name)
        [:br]))

(defn registration-page []
  (layout/common
    (form-to [:post "/register"]
      (control text-field :id "screen name")
      (control password-field :pass "Password")
      (control password-field :pass1 "Retype Password")
      (submit-button "Create Account"))))

我的问题是:在替代代码中,为什么参数名的值不是字符串?例如,为什么是(控制文本字段:id“屏幕名称”),而不是(控制文本字段“id”“屏幕名称”)?

我不熟悉Hiccup,也没有你提到的那本书。但通过阅读Hiccup源代码,您可以发现:

正在调用它调用的函数。看看这个函数,看看它在做什么

(defn ^String as-str
  "Converts its arguments into a string using to-str."
  [& xs]
  (apply str (map to-str xs)))
这将引导您使用ToString协议

在您发布的代码段中传递字符串而不是关键字,然后查看发生了什么


源代码是我们能拥有的最好的文档

我不熟悉Hiccup,也没有你提到的那本书。但通过阅读Hiccup源代码,您可以发现:

正在调用它调用的函数。看看这个函数,看看它在做什么

(defn ^String as-str
  "Converts its arguments into a string using to-str."
  [& xs]
  (apply str (map to-str xs)))
这将引导您使用ToString协议

在您发布的代码段中传递字符串而不是关键字,然后查看发生了什么

源代码是我们能拥有的最好的文档