Last active
December 14, 2015 19:49
-
-
Save methodin/5139881 to your computer and use it in GitHub Desktop.
Compojure example with Enlive template inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns list.handler | |
(:use compojure.core | |
ring.adapter.jetty | |
[net.cgrand.enlive-html :as html]) | |
(:require [compojure.handler :as handler] | |
[compojure.route :as route]) | |
(:gen-class)) | |
(defmacro maybe-substitute | |
([expr] `(if-let [x# ~expr] (html/substitute x#) identity)) | |
([expr & exprs] `(maybe-substitute (or ~expr ~@exprs)))) | |
(defmacro maybe-content | |
([expr] `(if-let [x# ~expr] (html/content x#) identity)) | |
([expr & exprs] `(maybe-content (or ~expr ~@exprs)))) | |
(deftemplate base-view "views/base.html" | |
[{:keys [content]}] | |
[:#content] (maybe-substitute content)) | |
(html/defsnippet test-view "views/test.html" [:div#content] | |
[{:keys [left middle right]}] | |
[:div#left] (maybe-substitute left) | |
[:div#middle] (maybe-substitute middle) | |
[:div#right] (maybe-substitute right)) | |
(html/defsnippet user-view "views/user.html" [:div#content] | |
[{:keys [id]}] | |
[:span#user-id] (maybe-substitute id)) | |
(defn render | |
([tmpl] (tmpl {})) | |
([tmpl repl] | |
(base-view {:title "Clojure List" | |
:content (tmpl repl)}))) | |
(defroutes app-routes | |
(GET "/test" [] (render test-view {})) | |
(GET ["/user/:id", :id #"[0-9]+"] [id] (render user-view {:id id})) | |
(route/not-found "Not Found")) | |
(def app | |
(handler/site app-routes)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment