Skip to content

Commit f888ac7

Browse files
author
Joy Clark
committed
Update to hiccup2.html
1 parent 31517ce commit f888ac7

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

todo-app/cheats/01/handler.clj

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
(ns todo-app.handler
22
(:require [compojure.core :refer [defroutes GET POST DELETE]]
33
[compojure.route :as route]
4-
[hiccup.page :refer [html5 include-css]]
4+
[hiccup.page :refer [doctype include-css]]
5+
[hiccup2.core :refer [html]]
56
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]))
67

78
(defn page [title & content]
8-
(html5
9-
[:head
10-
[:title title]
11-
(include-css "splendor.css")]
12-
[:body content]))
9+
(str
10+
(html
11+
(doctype :html5)
12+
[:html
13+
[:head
14+
[:title title]
15+
(include-css "splendor.css")]
16+
[:body content]])))
1317

1418
(defn index []
1519
(page "TODO App"

todo-app/test/todo_app/01_tests.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
(deftest styles-loaded
1717
(testing "css stylesheet linked"
1818
(let [html (page "Some title")]
19-
(is (str/includes? html "<link href=\"splendor.css\" rel=\"stylesheet\" type=\"text/css\">")))))
19+
(is (str/includes? html "<link href=\"splendor.css\" rel=\"stylesheet\" type=\"text/css\" />")))))
2020

2121
(deftest test-app
2222
(testing "main route"

todo-app/tests/01_tests.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
(deftest styles-loaded
1717
(testing "css stylesheet linked"
1818
(let [html (page "Some title")]
19-
(is (str/includes? html "<link href=\"splendor.css\" rel=\"stylesheet\" type=\"text/css\">")))))
19+
(is (str/includes? html "<link href=\"splendor.css\" rel=\"stylesheet\" type=\"text/css\" />")))))
2020

2121
(deftest test-app
2222
(testing "main route"

tutorial/chapters/00_instructions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ If you are reading this tutorial, the chances that you have downloaded the corre
44

55
Congratulations!
66

7-
If you are viewing this tutorial in a browser, the chances that you have already installed [leinigen](http://github.com/technomancy/leinigen) are also quite high. If this is not the case, please do this right now.
7+
If you are viewing this tutorial in a browser, the chances that you have already installed [leinigen](http://github.com/technomancy/leinigen) are also quite high. If you are using the [Nightcode IDE](https://sekao.net/nightcode/) it comes bundled with leinigen so you should be fine. If this is not the case, please do this right now.
88

99
During this tutorial we want to have a hands on experience with Clojure and build a Clojure web application from the base up.
1010

@@ -14,7 +14,7 @@ The app was created using the compojure leinigen template
1414

1515
lein new compojure todo-app
1616

17-
If you are using the [Nightcode IDE](https://sekao.net/nightcode/) for developing Clojure (which I recommend, especially when starting out with the language), you can start the application directly from the IDE.
17+
If you are using the [Nightcode IDE](https://sekao.net/nightcode/) for developing Clojure (which I recommend, especially when starting out with the language), you can start the application directly from the IDE (by hitting the `Run` button).
1818

1919
Otherwise, you can start the app from the command line:
2020

tutorial/chapters/01_instructions.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Basics, HTML Templating, and Namespaces
22

3-
If you have started the `todo-app` with `lein ring server` you can see what the app looks like at `http://localhost:3000`
3+
If you have started the `todo-app` with `lein ring server` or from Nightcode, you can see what the app looks like at `http://localhost:3000`
44

5-
The app currently only has a single route '/' which returns a message in text format. We want to return HTML instead of text, so let's look at what resources Clojure has to help us with this.
5+
The app currently only has a single route `/` which returns a message in text format. We want to return HTML instead of text, so let's look at what resources Clojure has to help us with this.
66

77
## Basics
88

@@ -75,12 +75,15 @@ In Clojure we have something like a list (Vectors!) and something like a name (K
7575
[:h1 "Title"]
7676
"Some content!"]
7777

78-
Hiccup provides the `hiccup.page/html5` element for generating an HTML5 page from such a data structure. This can also be combined with functions to have reusable templates!
78+
Hiccup provides the some built in elements that we can use out of the box. The `hiccup.page/doctype` function, for instance, can be used to generate the doctype for an HTML5 page. We can use the `hiccup2.core/html` function to generate the final HTML (HTML escaping by default was only introduced in Hiccup 2...but better late than never! For this reason, use the `hiccup2.core/html` function instead of the `hiccup.core/html` function which is still included for compatibility reasons). The `hiccup2.core/html` emits a `raw string` that we can then transform to a string with the `str` function. We can now write a function that will be a reusable page template!
7979

8080
(defn page [title & content]
81-
(html5
82-
[:head [:title title]]
83-
[:body content]))
81+
(str
82+
(html
83+
(doctype :html5)
84+
[:html
85+
[:head [:title title]]
86+
[:body content]]))
8487

8588
The `&` in the function specifies that the function takes a variable number of arguments. The first argument `title` is required, and any others that are passed in will be bound to `content` as a list. In Hiccup, you can simply insert the list of elements, and these will be rendered correctly within the body tag.
8689

0 commit comments

Comments
 (0)