Skip to content

Commit

Permalink
Added defhtml macro
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Jun 14, 2010
1 parent 780ddc5 commit 0f8d0f4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/hiccup/core.clj
Expand Up @@ -245,6 +245,17 @@
~(make-html content)))
(make-html (cons options content)))))

(defmacro defhtml
"Define a function, but wrap its output in an implicit html macro."
[name & fdecl]
(let [[fhead fbody] (split-with #(not (or (list? %) (vector? %))) fdecl)
wrap-html (fn [[args & body]] `(~args (html ~@body)))]
`(defn ~name
~@fhead
~@(if (vector? (first fbody))
(wrap-html fbody)
(map wrap-html fbody)))))

(defn add-optional-attrs
"Add an optional attribute argument to a function that returns a vector tag."
[func]
Expand Down
12 changes: 12 additions & 0 deletions test/hiccup/test/core.clj
Expand Up @@ -109,6 +109,18 @@
(is (= (html {:mode :sgml} [:html [:link] (list [:link])])
"<html><link><link></html>"))))

(deftest defhtml-macro
(testing "basic html function"
(defhtml basic-fn [x] [:span x])
(is (= (basic-fn "foo") "<span>foo</span>")))
(testing "html function with overloads"
(defhtml overloaded-fn
([x] [:span x])
([x y] [:span x [:div y]]))
(is (= (overloaded-fn "foo") "<span>foo</span>"))
(is (= (overloaded-fn "foo" "bar")
"<span>foo<div>bar</div></span>"))))

(deftest defelem-macro
(testing "one overload function"
(defelem one-form-two-args [a b] [b a 3])
Expand Down

0 comments on commit 0f8d0f4

Please sign in to comment.