By the end of this tutorial you should be able to do the following
- create a CHP project
- create a CHP CHTML page
- define a CHP route
- Clone the repository
git clone https://github.com/runexec/chp- CHP now sits in the directory chp, but you do not want to modify this copy. You should copy the chp folder into another folder holding the name of your project.
mkdir hello-world
cp -r chp/ hello-world/
tree -d hello-world/hello-world/
└── chp
├── chp-root
├── resources
│ └── public
├── src
│ └── chp
├── target
│ ├── classes
│ └── stale
├── test
│ └── chp
│ └── test
└── tutorial
└── 01
- Your hello-world CHP project is now setup correctly and you can run the ring server.
cd hello-world/chp/; lein ring server
2013-05-09 12:46:45.889:INFO:oejs.Server:jetty-7.6.1.v20120215
2013-05-09 12:46:45.947:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:3000
Started server on port 3000- First thing we're going to do is delete the contents of
hello-world/chp-root/test-file.chtml - In the same file, paste the following HTML code into the CHTML file.
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>- To access this blank page, you can visit http://localhost:3000/chtml
- Let's give this page an interesting title.
<html>
<head>
<title>
<clj>
(println
(format "%s's ClojureHomePage!"
(escape
(System/getProperty "user.name")))
</clj>
</title>
</head>
<body>
</body>
</html>If you did everything correctly, the title should display your current logged in username before displaying "ClojureHomePage!". 5. Now let's have a little fun with the CHTML body.
<html>
<head>
<title>
<clj>
(println
(format "%s's ClojureHomePage!"
(escape
(System/getProperty "user.name"))))
</clj>
</title>
</head>
<body>
<table>
<tr>
<td><h1>System Property</h1></td>
<td><h1>Property Value</h1></td>
</tr>
<clj>
(doseq [_ (System/getProperties)
:let [k (key _)
v (System/getProperty k)]]
(println
(format "<tr><td>%s</td><td>%s</td>"
(escape k)
(escape v))))
</clj>
</table>
</body>
</html>- Now that you've created your own CHTML page, let's start to give it a better route by opening the file
hello-world/chp/src/chp/handler.clj - You'll want to scroll to the very bottom of the file or until you see
(defroutes app-routes ...) - Listed here are all the possible ways to make a CHP route.
(defroutes app-routes
(chp-route "/chtml"
(binding [*title* "Test Page Example"]
(or (chp-parse (str root-path "test-page.chtml"))
"error")))
(chp-route "/"
(let [display (str "Method " ($ method) "<br />"
"URI " ($ uri) "<br />"
"Params " ($ params) "<br />")]
(chp-body {:-get (str "Get => " display)
:-post (str "Post => " display)
:-not-found "Sorry, but this page doesn't exist"})))
(chp-route "/testing"
(or (chp-when :get
(format "chp-body wasn't used to access %s from %s with %s"
($ uri) ($ ip) ($ headers)))
"Not Found"))
(chp-route "/chp"
(or (chp-parse (str root-path "chp-info.chtml"))
"error"))
(route/resources "/")
(route/not-found "Not Found"))- Let's make the simplest possible route we can for the location of "/"
(defroutes app-routes
(chp-route "/"
(or (chp-parse (str root-path "test-page.chtml"))
"Page not found!"))
(route/resources "/")
(route/not-found "Not Found"))- After saving the changes, point your browser to http://localhost:3000/