help chaining transformations

52 views
Skip to first unread message

Vaso Vukotic

unread,
Feb 20, 2015, 1:56:20 PM2/20/15
to enliv...@googlegroups.com
Hello, i'm new to Enlive, so far I like it a lot.  Basically I see that with enlive you can take an HTML page, turn it into a collection of enlive nodes, manipulate them, and then change them back to HTML via emit*

So far I have a page that uses 3 transformations.  As I will be increasing the number of transformations I would like to get a good grasp on how the enlive-html/do-> chaining operator works.

If I want to chain a bunch of transformations together and also pass each transformation a different piece of data to use, what does it look like?

Vaso Vukotic

unread,
Feb 20, 2015, 2:55:38 PM2/20/15
to enliv...@googlegroups.com
Apologies to double post, but it seems like I know how to approach my issue; it seems like defsnippet is the way to go because I can select chunks of my page to work upon.  How do you combine multiple "snippets" into a cohesive html page?  I'm unable to locate any examples. 

Furthermore, can I use snippets to dynamically generate elements?


e.g.
    (html/defsnippet global-page "global.html"
      [:.postpool] ;;operating on the subset of nodes with class "postpool"
      [posts]  ;;passing in posts as an argument from invokation
      [:post-holder] (eh/clone-for [i (range 12)] 
(eh/do->
(eh/content (post-content i))))

I assume the above could be used to populate #post-holder with 12 cloned divs... if I create some more similar snippets, how can I then weave them together to create a full page?

Thanks for any help or assistance you may offer

Christophe Grand

unread,
Feb 20, 2015, 6:02:33 PM2/20/15
to enlive-clj
Some definitions that could help with your initial question:

do-> allows to chain transformations.

A transformation is a function of one node to a collection of node.
If you want to pass data to the transformation too then you have to create a function that returns a transformation such as:
(defn xxxx [data] (fn actual-transform [node] ...))

Snippets on the other hand depends only on data (their nodes have been set at definition time).

--
You received this message because you are subscribed to the Google Groups "Enlive" group.
To unsubscribe from this group and stop receiving emails from it, send an email to enlive-clj+...@googlegroups.com.
To post to this group, send email to enliv...@googlegroups.com.
Visit this group at http://groups.google.com/group/enlive-clj.
For more options, visit https://groups.google.com/d/optout.



--
On Clojure http://clj-me.cgrand.net/
Clojure Programming http://clojurebook.com
Training, Consulting & Contracting http://lambdanext.eu/

Vaso Vukotic

unread,
Feb 21, 2015, 12:11:02 PM2/21/15
to enliv...@googlegroups.com
I see.  Thank you very much Christophe.

So, something like

(eh/defsnippet blurb-snippet "global.html"
  [:.blurb]
  [blurbs]
  [:.blurb]   (eh/clone-for [i (range numberOfBlurbsToShow)]
                (eh/content (blurb-sample-content i))))


probably wouldn't work, because defsnippet doesn't generate nodes but just sets content?


Thanks for your help.  I'm wondering what the best way to do what I want is.. will keep experimenting :)




Vaso Vukotic

unread,
Feb 21, 2015, 1:55:29 PM2/21/15
to enliv...@googlegroups.com
Okay so I have a real question finally:

    (defn render-ct-html []
      (apply str (eh/emit* (header-content-transform))))

Now say I have several transforms:  body-content-transform, footer-content-transform...

How do I chain them together ?

I try doing something like

    (defn render-ct-html []
       (apply str (eh/emit* (eh/do->
                              (header-content-transform)
                              (body-content-transform)
                              (footer-content-transform)))))

But I get an error that looks like:


java.lang.IllegalArgumentException

Don't know how to create ISeq from: net.cgrand.enlive_html$do__GT_$fn__8952

Would anyone be so kind as to explain to me how to use do-> ? =)

thanks a lot! =)

Vaso Vukotic

unread,
Feb 21, 2015, 2:48:17 PM2/21/15
to enliv...@googlegroups.com
Wow nevermind! :D


I just noticed that I can do transformations on specific parts of the page and simply (concat transform1 transform2 transform3) then use emit* to turn them into html.

Some fine tuning and viola.

Thanks a lot for enlive, Christophe, it's very beautiful.
Reply all
Reply to author
Forward
0 new messages