Precompiled templates?

27 views
Skip to first unread message

Saul

unread,
Oct 28, 2009, 7:26:24 PM10/28/09
to Enlive
Hello,
I was looking through the source of Enlive and it seems to me that
it is possible to read in templates at compile time:

(defmacro template
"A template returns a seq of string."
([source args & forms]
`(comp emit* (snippet* (html-resource ~source) ~args ~@forms))))

Could become something like:

(defmacro template
"A template returns a seq of string."
([source args & forms]
(let [ html (html-resource ~source) ]
`(comp emit* (snippet* ~html ~args ~@forms))))

And similar optimisation could be done to snippet* to preprocess the
nodes at compile time (although I don't understand what is happening
enough to do this at the moment).

Would this be of interest to you if I tried to do this? My thinking is
it could lead to a performance improvement and it would be cool to do
templating in clojure using techniques that wouldn't be possible in
other languages...

Saul Hazledine

Meikel Brandmeyer

unread,
Oct 29, 2009, 5:28:09 AM10/29/09
to Enlive
Hi,

On Oct 29, 12:26 am, Saul <shaz...@gmail.com> wrote:

> Would this be of interest to you if I tried to do this? My thinking is
> it could lead to a performance improvement and it would be cool to do
> templating in clojure using techniques that wouldn't be possible in
> other languages...

... and it would require recompilation, whenever you change something
in the template. With the way it is implemented you can simply modify
the template without the requirement of re-compilation.

Sincerely
Meikel

Christophe Grand

unread,
Oct 29, 2009, 6:56:38 AM10/29/09
to enliv...@googlegroups.com
Hi Saul and welcome!

On Thu, Oct 29, 2009 at 12:26 AM, Saul <sha...@gmail.com> wrote:
>
> Hello,
>  I was looking through the source of Enlive and it seems to me that
> it is possible to read in templates at compile time:
>
> (defmacro template
>  "A template returns a seq of string."
>  ([source args & forms]
>   `(comp emit* (snippet* (html-resource ~source) ~args ~@forms))))
>
> Could become something like:
>
> (defmacro template
>  "A template returns a seq of string."
>  ([source args & forms]
>    (let [ html    (html-resource ~source) ]
>       `(comp emit* (snippet* ~html ~args ~@forms))))

In earlier revisions, Enlive was less dynamic than now. I was trying
to do too much at compile time, including reading templates... at the
cost of flexibility.
One obvious gain is the one stated by Meikel: you can have a compiled
clojure app and "theme" it by replacing/customizing html files.
Generally, reading templates at compile-time precludes dynamic
sources. (eg removing unnecessary nodes, using a dynamic selector to
select a widget, etc.).

On perfs: the source backing the template is read when the template is
initialized, not at each invocation so it's not really compile-time vs
run-time but compile-time vs init-time.

Using the current (dynamic) building bricks provided by Enlive, it may
well be possible, and even interesting, to construct a more static
system like the one you envision but I won't change the current
template and snippet since it would reduce their applicability.

Christophe

--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)

Reply all
Reply to author
Forward
0 new messages