The reasons why Enlive templates return seqs of strings

unsorted — cgrand, 10 February 2010 @ 12 h 03 min

The main reasons behind Enlive templates returning seqs of strings are:

  • Ring does not expose the output stream (and I think it is a good thing)
  • I don’t want to allocate the whole response as a single string

Hence it seemed to me the simplest thing to do. (Simpler than implementing InputStream or making templates and Ring to communicate through a kind of reduce-based IO.)

Nodes serializations are cached when the source of the template is read:

user=> (deftemplate hello (-> "<h1>This is a static title<h2>This is a dynamic title" java.io.StringReader. html-resource) [name] [:h2] (content name))
#'user/hello
user=> (hello "Replaced dynamic title")
("<html>" "<body>" "<h1>This is a static title</h1>" "<h2>" "Replaced dynamic title" "</h2>" "</body>" "</html>")
user=> (hello "Replaced dynamic title II")
("<html>" "<body>" "<h1>This is a static title</h1>" "<h2>" "Replaced dynamic title II" "</h2>" "</body>" "</html>")
user=> (map identical? *1 *2)
(true true true true false true true true)

Thus no string allocation except for the dynamic parts but the real raison d’être of the static nodes serializations cache is to reduce the time spent traversing the tree and the number of allocated Cons (since the strings are longer).

Even without this cache there’s no String allocation when serializing a tree (but many Cons instances are allocated):

user=> (let [x {:tag :html :content [{:tag :body :content [{:tag :h1 :content ["This is a static title"]} {:tag :h2 :content ["This is a dynamic title"]}]}]}]
         (every? (partial apply identical?) (map vector (emit* x) (emit* x))))
true

3 Comments »

  1. [...] The reasons why Enlive templates return seqs of strings Weitere themenbezogene Artikel:Clojure – Destillat #2Funding Clojure 2010Clojure – Destillat #4Clojure Screencasts Teil IIClojure – Destillat #1Powered by Contextual Related PostsVN:F [1.8.5_1061]please wait…Rating: 0.0/10 (0 votes cast) verlinkt mich / drucken: [...]

  2. field shelters for sale

    Clojure and me » The reasons why Enlive templates return seqs of strings

    Trackback by field shelters for sale — 3 January 2015 @ 18 h 46 min
  3. Hello colleagues, how is the whole thing, and what you would like to say regarding
    this piece of writing, in my view its truly awesome
    in favor of me.

    Comment by planningitsmarter.com — 4 January 2015 @ 4 h 13 min

RSS feed for comments on this post. TrackBack URI

Leave a comment

(c) 2024 Clojure and me | powered by WordPress with Barecity