Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/weavejester/hiccup
Fast library for rendering HTML in Clojure
https://github.com/weavejester/hiccup
clojure hiccup html
Last synced: 4 days ago
JSON representation
Fast library for rendering HTML in Clojure
- Host: GitHub
- URL: https://github.com/weavejester/hiccup
- Owner: weavejester
- License: epl-1.0
- Created: 2009-11-02T21:08:16.000Z (about 15 years ago)
- Default Branch: master
- Last Pushed: 2024-07-22T11:18:14.000Z (5 months ago)
- Last Synced: 2024-10-29T15:11:38.791Z (about 1 month ago)
- Topics: clojure, hiccup, html
- Language: Clojure
- Homepage: http://weavejester.github.io/hiccup
- Size: 455 KB
- Stars: 2,691
- Watchers: 54
- Forks: 177
- Open Issues: 27
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
Awesome Lists containing this project
- stars - weavejester/hiccup - Fast library for rendering HTML in Clojure \[*Eclipse Public License 1.0*\] (⭐️2694) (Clojure)
- stars - weavejester/hiccup - Fast library for rendering HTML in Clojure \[*Eclipse Public License 1.0*\] (⭐️2689) (Clojure)
README
# Hiccup [![Build Status](https://github.com/weavejester/hiccup/actions/workflows/test.yml/badge.svg)](https://github.com/weavejester/hiccup/actions/workflows/test.yml)
Hiccup is a library for representing HTML in Clojure. It uses vectors
to represent elements, and maps to represent an element's attributes.## Install
Add the following dependency to your `deps.edn` file:
hiccup/hiccup {:mvn/version "2.0.0-RC4"}
Or to your Leiningen `project.clj` file:
[hiccup "2.0.0-RC4"]
## Documentation
* [Wiki](https://github.com/weavejester/hiccup/wiki)
* [API Docs](http://weavejester.github.io/hiccup)## Syntax
Here is a basic example of Hiccup syntax:
```clojure
user=> (require '[hiccup2.core :as h])
nil
user=> (str (h/html [:span {:class "foo"} "bar"]))
"bar"
```The first element of the vector is used as the element name. The second
attribute can optionally be a map, in which case it is used to supply
the element's attributes. Every other element is considered part of the
tag's body.Hiccup is intelligent enough to render different HTML elements in
different ways, in order to accommodate browser quirks:```clojure
"
user=> (str (h/html [:script]))
""
user=> (str (h/html [:p]))
"
```And provides a CSS-like shortcut for denoting `id` and `class`
attributes:```clojure
"
user=> (str (h/html [:div#foo.bar.baz "bang"]))
"
```If the body of the element is a seq, its contents will be expanded out
into the element body. This makes working with forms like `map` and
`for` more convenient:```clojure
user=> (str (h/html [:ul
(for [x (range 1 4)]
[:li x])]))
"
- 1
- 2
- 3
```
Strings are automatically escaped:
```clojure
user=> (str (h/html [:p "Tags in HTML are written with <>"]))
"
Tags in HTML are written with <>
"```
To bypass this, use the `hiccup2.core/raw` function:
```clojure
user=> (str (h/html [:p (h/raw "Hello World")]))
"
Hello World
"```
This can also be used for doctype declarations:
```clojure
user=> (str (h/html (h/raw "") [:html {:lang "en"}]))
""
```
## Differences between Hiccup 1 and 2
In brief: Hiccup 1 doesn't escape strings by default, while Hiccup 2
does. They occupy different namespaces to ensure backward compatibility.
In Hiccup 1, you use the `h` function to escape a string - that is,
ensure that unsafe characters like `<`, `>` and `&` are converted into
their equivalent entity codes:
```clojure
(h1/html [:div "Username: " (h1/h username)])
```
In Hiccup 2 strings are escaped automatically, but the return value from
the `html` macro is a `RawString`, rather than a `String`. This ensures
that the `html` macro can still be nested.
```clojure
(str (h2/html [:div "Username: " username]))
```
It's recommended to use Hiccup 2 where possible, particularly if your
app handles user data. Use of the non-core namespaces, such as
`hiccup.page`, should be avoided with Hiccup 2.
## License
Copyright © 2023 James Reeves
Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version.