Skip to content

Instantly share code, notes, and snippets.

@rlander
Created April 17, 2012 19:45
Show Gist options
  • Save rlander/2408554 to your computer and use it in GitHub Desktop.
Save rlander/2408554 to your computer and use it in GitHub Desktop.
Old hiccup demo
(ns hiccup-demo
(:use hiccup))
(def core-examples
["Simple tags" [:br]
"Tags with content" [:span "baz"]
"Tag with attributes" [:span {:id "foo" :class "bar"} "baz"]
"Attribute shortcuts" [:span#foo.bar "baz"]
"Encode HTML character entities" '(escape-html "<\"foo\"&\"bar\">")
"Shortcut for escape-html" '(h "<\"foo\"&\"bar\">")])
(def page-helpers-examples
["Doctypes: html4, html5, xhtml-strict, xhtml-transitional" '(doctype :html4)
"XHTML tag" '(xhtml-tag "en" "foo")
"Including JavaScript files" '(include-js "foo.js" "bar.js")
"Including CSS files" '(include-js "foo.css" "bar.css")
"Embedded JavaScript tag (with CDATA section)" '(javascript-tag "document.write('hello world');")
"Hyperlink shorcut" '(link-to "http://foo.bar" "baz")
"Unordered list" '(unordered-list ["foo" "bar" "baz"])
"Ordered list" '(ordered-list ["foo" "bar" "baz"])])
(def form-helpers-examples
["Form tag" '(form-to [:POST "/foo"] "...")
"Label" '(label :foo "bar")
"Hidden field" '(hidden-field :foo "bar")
"Text field" '(text-field :foo "bar")
"Password field" '(password-field :foo "bar")
"Checkbox" '(check-box :foo true "bar")
"Radio button" '(radio-button :foo true "bar")
"Option tag" '(select-options [:foo :bar :baz] :bar)
"Drop down list" '(drop-down :foo [:bar :baz] :baz)
"Text area" '(text-area :foo "bar")
"File upload" '(file-upload :foo)
"Submit button" '(submit-button "submit")
"Reset button" '(submit-button "reset")])
(defn ->table [namespace examples]
(html
[:h2 namespace]
[:table
(map (fn [[t e]]
(html [:tr
[:td t]
[:td (str e)]
[:td (h (html (eval e)))]]))
(partition 2 examples))]))
(defn hiccup-cheatsheet [title]
(html
(doctype :xhtml-strict)
(xhtml-tag "en"
[:head
[:title title]
[:style {:type "text/css"}
"body{background:#222;color:#ddd;margin:1em}"
"pre{border:1px solid;padding:1em}"
"table{width:100%;border-spacing:0}"
"td{padding:0.3em;border:1px solid #666}"]]
[:body [:h1 title] [:hr]
(->table "hiccup.core" core-examples)
(->table "hiccup.page-helpers" page-helpers-examples)
(->table "hiccup.form-helpers" form-helpers-examples)
[:h2 "Code to Generate this Cheatsheet"]
[:pre (slurp "hiccup_demo.clj")]
[:h2 "Command to Generate this Cheatsheet"]
[:pre "java -cp \"lib/clojure-1.1.0.jar:lib/clojure-contrib-1.1.0.jar:hiccup.jar\" clojure.main hiccup_demo.clj > hiccup_cheatsheet.html"]])))
(println (hiccup-cheatsheet "Hiccup Cheatsheet"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment