Common page elements; page decoration vs. nested templates

31 views
Skip to first unread message

Robert Campbell

unread,
Nov 23, 2009, 4:05:54 PM11/23/09
to enliv...@googlegroups.com
Imagine you have a common header and footer. One way I imagine
implementing this is to (deftemplate header) and (deftemplate footer),
then manually include them on ever page:

(deftemplate index "index.html" []
[:div#header] (html-content (header x y z etc))
[:#other_stuff] (content "body stuff")
[:#footer] (html-content (footer z y etc))

The other way I can image is through Compojure decorators:

(decorate my-app
(with-header (header x y z))
(with-footer (footer x y z)))

Does anyone have experience with these two methods? Any thoughts?

Christophe Grand

unread,
Nov 24, 2009, 2:58:56 AM11/24/09
to enliv...@googlegroups.com
Hi,

On Mon, Nov 23, 2009 at 10:05 PM, Robert Campbell <rrc...@gmail.com> wrote:
Imagine you have a common header and footer. One way I imagine
implementing this is to (deftemplate header) and (deftemplate footer),
then manually include them on ever page:

(deftemplate index "index.html" []
   [:div#header] (html-content (header x y z etc))
   [:#other_stuff] (content "body stuff")
   [:#footer] (html-content (footer z y etc))

As soon as you consider to reuse a part of a template, you must define the part as a snippet, not as a template. Templates are only for top-level, they return a html serialization. Snippets return trees.

One way to achieve your goal is to define your header and footer in a file (say "layout.html") and to use this file as a source for your template. From there there are two main alternatives:
* either you pass each component of the page as an argument, eg:

(deftemplate layout "layout.html" [title main]
  [:#title] (content title)
  [:#content] (substitute main))

(where main can be a tree)

* or your give it a single argument: the page to be decorated (the page itself being rendered by a snippet)
(deftemplate layout "layout.html" [page]
  [:#title] (content (select page [:title]))
  [:#content] (substitute (select page [:body])))

(you may have to also copy meta/script/style tags)

Christophe
Reply all
Reply to author
Forward
0 new messages