Skip to content

Instantly share code, notes, and snippets.

@ibdknox
ibdknox / hiccup.clj
Created June 27, 2011 20:58
generating html in noir
(use 'hiccup.core)
;; ****************************************************************
;; The basics
;; ****************************************************************
(html [:p])
=> "<p />"
;; Any elements after that become the content of the tag
@ibdknox
ibdknox / defpartial.clj
Created June 27, 2011 21:50
defpartial source
(defmacro defpartial
"Create a function that returns html using hiccup. The function is
callable with the given name."
[fname params & body]
`(defn ~fname ~params
(html
~@body)))
(defpartial hello [person]
[:p "Hello " person])
@ibdknox
ibdknox / map.clj
Created June 28, 2011 08:06
defpartial with map
(defpartial post-item [{:keys [perma-link title md-body date tme]}]
[:li.post
[:h2 (link-to perma-link title)]
[:ul.datetime
[:li date]
[:li tme]]
[:div.content md-body]])
(defpartial posts-list [items]
[:ul.posts
(defn my-bot []
(doseq [ant (my-ants)]
(let [f (closest ant (food))
food-dirs (shuffle (direction ant f))
dirs (concat food-dirs (shuffle directions))]
(when-let [dir (first (drop-while #(not (valid-move? ant %)) dirs))]
(move ant dir)))))
@ibdknox
ibdknox / complexparams.clj
Created July 24, 2011 20:27
basic defpage
(defpage [:get ["/user/:id" :id #"\d+"]] {:keys [id]}
(str "You are user number " id))
(defpartial error-item [[first-error]]
[:p.error first-error])
(defpartial user-fields [{:keys [firstname lastname]}]
(vali/on-error :firstname error-item)
(label "firstname" "First name: ")
(text-field "firstname" firstname)
(vali/on-error :lastname error-item)
(label "lastname" "Last name: ")
(text-field "lastname" lastname))
@ibdknox
ibdknox / cookies.clj
Created July 25, 2011 00:17
sessions and cookies
(require '[noir.cookies :as cookies])
(cookies/put! :user-id 2)
(cookies/get :user-id)
;; => 2
(cookies/get :username "anon")
;; => "anon"
;; you can also pass a map that has all the cookie's attributes
@ibdknox
ibdknox / genhandler.clj
Created July 25, 2011 01:07
gen-handler
(require '[noir.server :as server])
(server/load-views "src/noir-example/views")
(def handler (server/gen-handler {:mode :dev
:ns 'noir-example}))
@ibdknox
ibdknox / add.clj
Created July 25, 2011 03:44
middleware
(server/add-middleware my-middleware)
#(:longest
(reduce
(fn [{:keys [longest cur] :as res} n]
(let [{:keys [cur] :as updated} (if (or (not (seq cur))
(> n (last cur)))
(update-in res [:cur] conj n)
(assoc res :cur [n]))
longest-cnt (count longest)
cur-cnt (count cur)]
(if (and (> cur-cnt 1)