Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created March 4, 2010 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cgrand/321661 to your computer and use it in GitHub Desktop.
Save cgrand/321661 to your computer and use it in GitHub Desktop.
(ns enlive-mw.core
(:use compojure)
(:require [net.cgrand.enlive-html :as e]))
(e/deftemplate simple-layout "enlive_mw/layout.html"
[{:keys [title ps widget]}]
#{[:title] [:h1]} (e/content title)
[:p] (e/clone-for [p ps] (e/content p))
[:#widget] (e/content widget))
(e/defsnippet widget "enlive_mw/widget.html" [:div] [s]
[:b] (e/content s))
(defn welcome-page [req]
{:body {:title "Layout through middleware: Demo"
:ps ["Lorem ipsum dolor sit amet, consectetur adipiscing elit."
"Ut hendrerit, nibh ac porttitor rhoncus, diam dolor
vestibulum nisi, non suscipit urna est vitae est."]
:widget (widget "Hello World")}})
(defn layout-with [handler layout]
(fn [req]
(let [{body :body :as resp} (handler req)]
(if (map? body)
(assoc resp :body (layout body))
resp))))
(decorate welcome-page
(layout-with simple-layout))
(defroutes app
(GET "/" welcome-page)
(ANY "*"
[404 "Page Not Found"]))
(defn start-app
([] (start-app 8080))
([port] (start-app app port))
([app port]
(run-server {:port port}
"/*" (servlet app))))
<html>
<head>
<title>Title goes here</title>
</head>
<body>
<h1>Title goes here too</h1>
<p>A very intersting paragraph</p>
<div id="widget">intentionally left blank</div>
</body>
</html>
<html>
<head>
<title>Layout through middleware: Demo</title>
</head>
<body>
<h1>Layout through middleware: Demo</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p><p>Ut hendrerit, nibh ac porttitor rhoncus, diam dolor
vestibulum nisi, non suscipit urna est vitae est.</p>
<div id="widget"><div style="background: #ccc; font: bold">I'm a wonderful widget which says <b>Hello World</b>!</div></div>
</body>
</html>
<html>
<body>
<div style="background: #ccc; font: bold">I'm a wonderful widget which says <b>boo</b>!</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment