@@ -852,9 +852,9 @@ returns a Hiccup data structure.
852852
853853(defn index [_options]
854854 (fn [_request]
855- [:html {:lang "en"}
856- [:head [:title "Hello World Wide Web"]]
857- [:body [:h1 "Hello World Wide Web"]]]))
855+ {:body [:html {:lang "en"}
856+ [:head [:title "Hello World Wide Web"]]
857+ [:body [:h1 "Hello World Wide Web"]]]} ))
858858----
859859
860860Finally, we trigger a `(reset)` at the REPL.
@@ -932,42 +932,41 @@ Let's take a closer look at function associated with the route.
932932
933933(defn index [_options]
934934 (fn [_request]
935- [:html {:lang "en"}
936- [:head [:title "Hello World Wide Web"]]
937- [:body [:h1 "Hello World Wide Web"]]]))
935+ {:body [:html {:lang "en"}
936+ [:head [:title "Hello World Wide Web"]]
937+ [:body [:h1 "Hello World Wide Web"]]]} ))
938938----
939939
940940This function returns another function, known as a
941941https://github.com/ring-clojure/ring[Ring] handler. Usually this
942- function will return a response map, but in this case we're returning a
942+ function will return a full response map, but in this case we're
943+ omitting all but the `:body`, which contains a
943944https://github.com/weavejester/hiccup[Hiccup] vector.
944945
945946Hiccup is a format for representing HTML as a Clojure data structure.
946947Elements are represented by a vector starting with a keyword, followed
947948by an optional attribute map and then the element body.
948949
949950The `:hiccup` feature of the web module adds middleware to turn Hiccup
950- vectors into HTML response maps. If the response is a vector, it wraps
951- the vector in response map. If the response is already a map, it checks
952- the `:body` of the response for a vector.
951+ vectors into HTML response maps. If the response body is a vector, it
952+ coverts it to a string of HTML. The missing `:status` and `:headers`
953+ keys that are usually present in the response map are given default
954+ values.
953955
954- If we wanted a custom status code or headers, then the full response
955- map could be returned.
956+ The next example is equivalent, with the default values set explicitly.
956957
957958[,clojure]
958959----
959960(defn index [_options]
960961 (fn [_request]
961962 {:status 200
962- :headers {}
963+ :headers {"Content-Type" "text/html;charset=UTF-8" }
963964 :body [:html {:lang "en"}
964965 [:head [:title "Hello World Wide Web"]]
965966 [:body [:h1 "Hello World Wide Web"]]]}))
966967----
967968
968- NOTE: The `:status` and `:headers` keys map optionally be omitted.
969-
970- Or we could return the string directly:
969+ Which in turn is also equivalent to:
971970
972971[,clojure]
973972----
@@ -982,9 +981,6 @@ Or we could return the string directly:
982981</html>"}))
983982----
984983
985- All of these examples are equivalent, but returning a vector is the most
986- convenient and concise.
987-
988984
989985=== Middleware
990986
@@ -1233,9 +1229,9 @@ created earlier.
12331229
12341230(defn index [{:keys [db]}]
12351231 (fn [_request]
1236- [:html {:lang "en"}
1237- [:head [:title "Hello World Wide Web"]]
1238- [:body [:h1 "Hello World Wide Web"]]]))
1232+ {:body [:html {:lang "en"}
1233+ [:head [:title "Hello World Wide Web"]]
1234+ [:body [:h1 "Hello World Wide Web"]]]} ))
12391235----
12401236
12411237Before we go further, however, we should set up the database schema via
@@ -1337,11 +1333,11 @@ populate an unordered list. We'll change the index function accordingly.
13371333
13381334(defn index [{:keys [db]}]
13391335 (fn [_request]
1340- [:html {:lang "en"}
1341- [:head [:title "Todo"]]
1342- [:body
1343- [:ul (for [rs (jdbc/execute! db [list-todos])]
1344- [:li (:todo/description rs)])]]]))
1336+ {:body [:html {:lang "en"}
1337+ [:head [:title "Todo"]]
1338+ [:body
1339+ [:ul (for [rs (jdbc/execute! db [list-todos])]
1340+ [:li (:todo/description rs)])]]]} ))
13451341----
13461342
13471343TIP: It's often a good idea to factor out each SQL string into its own
@@ -1410,13 +1406,13 @@ codebase.
14101406
14111407(defn index [{:keys [db]}]
14121408 (fn [_request]
1413- [:html {:lang "en"}
1414- [:head [:title "Todo"]]
1415- [:body
1416- [:ul
1417- (for [rs (jdbc/execute! db [list-todos])]
1418- [:li (:todo/description rs)])
1419- [:li (create-todo-form)]]]]))
1409+ {:body [:html {:lang "en"}
1410+ [:head [:title "Todo"]]
1411+ [:body
1412+ [:ul
1413+ (for [rs (jdbc/execute! db [list-todos])]
1414+ [:li (:todo/description rs)])
1415+ [:li (create-todo-form)]]]]} ))
14201416
14211417(defn new-todo [{:keys [db]}]
14221418 (fn [{{:keys [description]} :params}]
@@ -1512,15 +1508,15 @@ our `index` function in the `todo.routes` namespace.
15121508----
15131509(defn index [{:keys [db]}]
15141510 (fn [_request]
1515- [:html {:lang "en"}
1516- [:head
1517- [:title "Todo"]
1518- [:script {:src "/cljs/client.js"}]]
1519- [:body
1520- [:ul
1521- (for [rs (jdbc/execute! db [list-todos])]
1522- [:li (:todo/description rs)])
1523- [:li (create-todo-form)]]]]))
1511+ {:body [:html {:lang "en"}
1512+ [:head
1513+ [:title "Todo"]
1514+ [:script {:src "/cljs/client.js"}]]
1515+ [:body
1516+ [:ul
1517+ (for [rs (jdbc/execute! db [list-todos])]
1518+ [:li (:todo/description rs)])
1519+ [:li (create-todo-form)]]]]} ))
15241520----
15251521
15261522If you restart the REPL and check http://localhost:3000, you should see
0 commit comments