Skip to content

Instantly share code, notes, and snippets.

@ossareh
Created January 3, 2011 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ossareh/763017 to your computer and use it in GitHub Desktop.
Save ossareh/763017 to your computer and use it in GitHub Desktop.
boilerplate with a ring middleware
(ns surveyengine.boilerplate
(:use [hiccup.core])
(:use [hiccup.page-helpers]))
(defn boilerplate
([] (boilerplate {} [] [] [] [] {:prod? false}))
([{:keys [title desc author favicon apple-icon cssver] :as head}
header
main
footer
javascript
;; descructure the vararg to capture the config keys we care about
& [{:keys [prod? gaq]} & _]]
(let [jq (str "jquery." (when prod? "min.") "js")]
(html (:html5 doctype)
(str
"<!--[if lt IE 7 ]> <html lang=\"en\" class=\"no-js ie6\"> <![endif]-->"
"<!--[if IE 7 ]> <html lang=\"en\" class=\"no-js ie7\"> <![endif]-->"
"<!--[if IE 8 ]> <html lang=\"en\" class=\"no-js ie8\"> <![endif]-->"
"<!--[if IE 9 ]> <html lang=\"en\" class=\"no-js ie9\"> <![endif]-->"
"<!--[if (gt IE 9)|!(IE)]><html lang=\"en\" class=\"no-js\"> <![endif]-->")
[:head
[:meta {:charset :utf-8}]
[:meta {:http-equiv :X-UA-Compatible
:content "IE=edge,chrome=1"}]
;; put a title in if we have one
(if title
[:title title]
[:title "New Page"])
;; put a description in if we have one
(when desc [:meta {:name :description
:content desc}])
;; put an author in if we have one
(when author [:meta {:name :author
:content author}])
[:meta {:name :viewport
:content "width=device-width, initial-scale=1.0"}]
(when favicon [:link {:rel "shortcut icon"
:href favicon}])
(when apple-icon [:link {:rel :apple-touch-icon
:href apple-icon}])
[:link {:rel :stylesheet
:href (str "/css/style.css?v=" (if cssver cssver 2))}]
(include-js "/js/libs/modernizr-1.6.min.js")]
[:body
[:div#container
[:header (when-not (empty? header) header)]
[:div#main (when-not (empty? main) main)]
[:footer (when-not (empty? footer) footer)]]
(include-js (str "//ajax.googleapis.com/ajax/libs/jquery/1.4.4/" jq))
[:script (str "!window.jquery && "
"document.write(unescape('%3Cscript src=\"/js/libs/"
jq
"\"%3E%3Cscript%3E'))")]
;; we have skipped the part about ant compiled scripts
;; that comes with html5boilerplate.
;; Instead you put the javascript you want included into
;; the javascript argument and it'll be included for you
(when-not (empty? javascript) javascript)
(str "<!--[if lt IE 7]>"
"<script src=\"/js/libs/dd_belatedpng.js\"></script>"
"<script>DD_belatedPNG.fix('img, .png_bg');</script>"
"<![endif]-->")
(when-not prod? (include-js "/js/libs/profiling/yahoo-profiling.min.js"
"/js/libs/profiling/config.js"))
(when gaq
[:script
(str "var _gaq = [['_setAccount', '" gaq "'],['_trackPageview']];"
"(function(d, t) {"
"var g = d.createElement(t), s = d.getElementsByTagName(t)[0];"
"g.async = true;"
"g.src = ('https:' == location.protocol ? "
"https://ssl' : http://www') + '.google-analytics.com/ga.js';"
"s.parentNode.insertBefore(g, s)})(document, 'script');")])]))))
(defn wrap-boilerplate [app config]
(fn [req]
(let [res (app req)
;; if the response has any of these keys then it is a
;; boilerplate response
res (if (some #{:head :header :main :footer :javascript} (keys res))
;; get the keys
(let [{:keys [head header main footer javascript]} res
;; clean up the response
res (dissoc res :head :header :main :footer :javascript)]
;; stash boilerplate output into :body, returning res
(assoc res
:body
(boilerplate head header main footer javascript config)))
;; res is passed through
res)]
res)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment