Clojure Web library hiccup(예전 compojure)
Programming clojure 책에는 compojure에 있는 html 함수들을 사용하는 예제들을 보여줍니다. 막상 해보니깐 안되더군요. 언제인지는 모르겠지만 hiccup이라는 라이브러리로 떨어져 나왔더군요.
Hiccup is a library for representing HTML in Clojure. It uses vectors to represent tags, and maps to represent a tag’s attributes.
hiccup의 readme 파일에 있는 내용입니다.(살짝 바뻐서, 대충 써야겠네요.) 예제소스를 보면 금방 알 수 있을 겁니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
(ns hello-www.core
(:use compojure.core
hiccup.core
ring.adapter.jetty)
(:require [compojure.route :as route]))
(defroutes example
(GET "/hello" []
(html
[:head
[:body
[:h1 "Hello World Wide Web!"]]]))
(GET "/" [] "<h1>root</h1>")
(GET "/sample" []
(html
[:head
[:title "html sample by hiccup lib, clojure"]
]
[:body
[:p [:a {:href "http://defree.co.kr"} "Samuel's blog"]]
[:p [:b "b tag"]]
[:p [:dd "dd tag"]]
[:p
[:div "form in a div tag"
[:form {:name "form1"}
[:p
[:input {:type "text" :name "text_input" :value "this is a text input tag."}]
[:input {:type "password" :name "password_input" :value "this is a password input tag."}]
[:input {:type "button" :name "button_input" :value "this is a button input tag."}]
[:input {:type "submit" :name "submit_input" :value "this is a submit input tag."}]
]
[:p
[:input {:type "radio" :name "text_input" :value "1" :selected "true"} "radio No.1"]
[:input {:type "radio" :name "text_input" :value "2" :selected "false"} "radio No.2"]
[:select {:name "select"}
[:option {:value "code1" :selected "true"} "select 1"]
[:option {:value "code2"} "select 2"]]
[:textarea {:value "textarea"}]]]]]
[:h1 "h1"]
[:h2 "h1"]
[:h3 "h2"]
[:h4 "h3"]
[:h5 "h4"]
[:h6 "h5"]]))
(route/not-found "Page not found"))
(run-jetty example {:port 8080}) |
꽤 쉽죠? 그리고 당연히 project.clj에 hiccup을 명시해 두어야겠죠.
1 2 3 4 5 6 7 8 | (defproject hello-www "0.0.1-SNAPSHOT"
:description "FIXME: write"
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]
[compojure "0.4.0"]
[ring/ring-jetty-adapter "0.2.3"]
[lein-eclipse "1.0.0"]
[hiccup "0.2.6"]]) |
마음이 급해져서 설명을 어떻게 할 지 생각나질 않는군요. -_-;;

