Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compojure Templating Pages

I have a bunch of static html files that share same header and footer. I would like to share this header and footer across all pages. For now i use the following routed but it is a bit ugly, and i have to take care of all special cases. Is there an easyway to dothis such as the php's include function?


(defroutes my-app
  (GET "/" 
    (html-with-template 
     "main.header"  "index.body" "main.footer" ))
  (GET "/*.html" 
    (html-with-template 
     "main.header" (str (params :*) ".body") "main.footer" ))
  (GET "/*/" 
    (html-with-template 
     (str (params :*) "/folder.header") 
     (str (params :*) "/index.body")
     (str (params :*) "/folder.footer")))
  (GET "/*" 
    (or (serve-file (params :*)) :next))
  (ANY "*"
    (page-not-found)))
like image 822
Hamza Yerlikaya Avatar asked Aug 27 '09 12:08

Hamza Yerlikaya


1 Answers

From what I've read about Compojure, I don't think it has inherent support for the concept of "auto prepend" and "auto append" to the response body like PHP does.

Other web frameworks with which I have experience delegate this responsibility to their templating engine, whereas PHP sort of blurs the lines a bit. They do this by allowing you to explicitly "include" a common snippet here, or render a macro, or even through rudimentary forms of inheritance (this template extends that template).

Basically, whether your HTML is static or dynamic, a templating engine allows you to modularize for better maintainability.

That said, Compojure doesn't appear to have a full-blown HTML templating engine bundled with it. It does have a nice little HTML/XML domain-specific language (DSL), but I think what you are looking for is more of a first class templating engine that can be used along with Compojure.

Enlive seems to be the Clojure-inspired templating engine that get the most hits, but I'm sure there are others. Given Clojure's JVM integration, you can probably pick from any of the Java-inspired templating engines as well.

Depending on which one you pick, there may be a few lines of glue code that you have to write to get your templates loaded, rendered and streamed into the Compojure HTTP responses, but you will write that once and reuse everywhere.

like image 122
Joe Holloway Avatar answered Oct 01 '22 00:10

Joe Holloway