0

在《使用 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"))))

我的问题是:在替代代码中,为什么参数名称的值不是字符串?例如,为什么是 (control text-field :id "screen name"),而不是 (control text-field "id" "screen name") ?

4

1 回答 1

5

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

标签正在调用它调用as-str 的make-id函数。看看那个函数,看看它在做什么。

(defn ^String as-str
  "Converts its arguments into a string using to-str."
  [& xs]
  (apply str (map to-str xs)))

这将引导您使用ToString协议。

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

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

于 2013-12-02T17:16:07.700 回答