Skip to content

Commit 5a6d45c

Browse files
committed
Update README with Hiccup 1/2 info
1 parent 82e199b commit 5a6d45c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,38 @@ user=> (str (h/html [:p (h/raw "Hello <em>World</em>")]))
7777
"<p>Hello <em>World</em></p>"
7878
```
7979

80+
This can also be used for doctype declarations:
81+
82+
```clojure
83+
user=> (str (h/html (h/raw "<!DOCTYPE html>") [:html {:lang "en"}]))
84+
"<!DOCTYPE html><html lang=\"en\"></html>"
85+
```
86+
87+
## Differences between Hiccup 1 and 2
88+
89+
In brief: Hiccup 1 doesn't escape strings by default, while Hiccup 2
90+
does. They occupy different namespaces to ensure backward compatibility.
91+
92+
In Hiccup 1, you use the `h` function to escape a string - that is,
93+
ensure that unsafe characters like `<`, `>` and `&` are converted into
94+
their equivalent entity codes:
95+
96+
```clojure
97+
(h1/html [:div "Username: " (h1/h username)])
98+
```
99+
100+
In Hiccup 2 strings are escaped automatically, but the return value from
101+
the `html` macro is a `RawString`, rather than a `String`. This ensures
102+
that the `html` macro can still be nested.
103+
104+
```clojure
105+
(str (h2/html [:div "Username: " username]))
106+
```
107+
108+
It's recommended to use Hiccup 2 where possible, particularly if your
109+
app handles user data. Use of the non-core namespaces, such as
110+
`hiccup.page`, should be avoided with Hiccup 2.
111+
80112
## License
81113

82114
Copyright © 2023 James Reeves

0 commit comments

Comments
 (0)