Hiccupでtableを出力する単純な関数を作ってみた
お遊びDSLの手始めに、Hiccupの機能拡張として単純なtable作成関数を作ってみた。
ソース
(defn table-header-detail [cols] (map #(vector :th (:name %)) cols)) (defn table-header [cols] [:tr (table-header-detail cols)]) (defn table-record-detail [record] (map #(vector :td %) record)) (defn table-record [record] [:tr (table-record-detail record)]) (defn table-records [records] (map #(table-record %) records)) (defn table [cols records & options] [:table (table-header cols) (table-records records)])
実行例
(use 'hiccup.core) (html [:html [:body (table [{:name "col1"} {:name "col2"}] [["val1-1" "val1-2"] ["val2-1" "val2-2"]])]]) ;-> "<html><body><table><tr><th>col1</th><th>col2</th></tr><tr><td>val1-1</td><td>val1-2</td></tr><tr><td>val2-1</td><td>val2-2</td></tr></table></body></html>"
ファイルに出力する例
(use 'hiccup.core 'clojure.java.io) (with-open [w (writer "out.html" :encoding "UTF-8")] (.write w (html [:html [:body (table [{:name "col1"} {:name "col2"}] [["val1-1" "val1-2"] ["val2-1" "val2-2"]])]])))
・・・これってJSTLの再発明??