Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fast library for rendering HTML in Clojure

License

NotificationsYou must be signed in to change notification settings

weavejester/hiccup

Repository files navigation

Hiccup is a library for representing HTML in Clojure. It uses vectorsto represent elements, and maps to represent an element's attributes.

Install

Add the following dependency to yourdeps.edn file:

hiccup/hiccup {:mvn/version "2.0.0"}

Or to your Leiningenproject.clj file:

[hiccup "2.0.0"]

Documentation

Syntax

Here is a basic example of Hiccup syntax:

user=> (require '[hiccup2.core:as h])niluser=> (str (h/html [:span {:class"foo"}"bar"]))"<span class=\"foo\">bar</span>"

The first element of the vector is used as the element name. The secondattribute can optionally be a map, in which case it is used to supplythe element's attributes. Every other element is considered part of thetag's body.

Hiccup is intelligent enough to render different HTML elements indifferent ways, in order to accommodate browser quirks:

user=> (str (h/html [:script]))"<script></script>"user=> (str (h/html [:p]))"<p />"

And provides a CSS-like shortcut for denotingid andclassattributes:

user=> (str (h/html [:div#foo.bar.baz"bang"]))"<div id=\"foo\" class=\"bar baz\">bang</div>"

If the body of the element is a seq, its contents will be expanded outinto the element body. This makes working with forms likemap andfor more convenient:

user=> (str (h/html [:ul                     (for [x (range14)]                       [:li x])]))"<ul><li>1</li><li>2</li><li>3</li></ul>"

Strings are automatically escaped:

user=> (str (h/html [:p"Tags in HTML are written with <>"]))"<p>Tags in HTML are written with &lt;&gt;</p>"

To bypass this, use thehiccup2.core/raw function:

user=> (str (h/html [:p (h/raw"Hello <em>World</em>")]))"<p>Hello <em>World</em></p>"

This can also be used for doctype declarations:

user=> (str (h/html (h/raw"<!DOCTYPE html>") [:html {:lang"en"}]))"<!DOCTYPE html><html lang=\"en\"></html>"

Differences between Hiccup 1 and 2

In brief: Hiccup 1 doesn't escape strings by default, while Hiccup 2does. They occupy different namespaces to ensure backward compatibility.

In Hiccup 1, you use theh function to escape a string - that is,ensure that unsafe characters like<,> and& are converted intotheir equivalent entity codes:

(h1/html [:div"Username:" (h1/h username)])

In Hiccup 2 strings are escaped automatically, but the return value fromthehtml macro is aRawString, rather than aString. This ensuresthat thehtml macro can still be nested.

(str (h2/html [:div"Username:" username]))

It's recommended to use Hiccup 2 where possible, particularly if yourapp handles user data. Use of the non-core namespaces, such ashiccup.page, should be avoided with Hiccup 2.

License

Copyright © 2025 James Reeves

Distributed under the Eclipse Public License either version 1.0 or (atyour option) any later version.

About

Fast library for rendering HTML in Clojure

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp