1

I am trying to define an enlive template for a html table, that shows data from a map. template-div for this example is right here. Dummy content that for the cells in the template is here.

defsnippet for cell value and deftemplate are defined as:

(require '[net.cgrand.enlive-html :as html])

(html/defsnippet value-cell (template-div) [:div.Row :div.Cell] [value]
          (html/content value))

However, when I try the snippet

(value-cell (mapv vals (:event-data dummy-content)))

All the values are in one tag like this

({:tag :div, :attrs {:class "Cell"}, 
:content ("end time 1" "date 1" "event name 1" "start time 1"  "performer 1" "end time 2" "date 2" "event name 2" "start time 2" "performer 2")})

And I need every item from a list to be a value in the tag.

claj
  • 5,172
  • 2
  • 27
  • 30
Vesna
  • 345
  • 2
  • 11

1 Answers1

1

You are passing a list of values to value-cell, so value-cell should look something like:

(html/defsnippet value-cell (template-div)
  [:div.Row :div.Cell]
  [values]
  (html/clone-for [value values]
                  (html/content value)))
DanLebrero
  • 8,545
  • 1
  • 29
  • 30
  • yes this is it, I was playing with clone-for but couldn't figure it out. – Vesna Oct 08 '14 at 11:33
  • Oh, and also, how to put the data from each map into separate row, because this (html/deftemplate mshp (index) [cont] [:div.Row] (html/content (map value-cell (mapv vals (:event-data dummy-content))))) doesn't work? – Vesna Oct 08 '14 at 11:41
  • it is the same problem. What you are telling enlive is "change the content of :div.Row for ...". What you want to tell it is "create one like :div.Row for each", which would translate for a clone-for – DanLebrero Oct 08 '14 at 12:45
  • yes, but, how to apply clone-for on every map, I need clone-for for cell, and then clone-for for row? – Vesna Oct 09 '14 at 15:10
  • If I understand you correctly, you want one :div.Row per entry on the map, not one :div.Cell per entry. If this is the case, what you want to clone is the whole :div.Row, so your value-cell snippet should be called, `value-row` and be something like (... [:div.Row] [event-data] (clone-for [[k v] event-data] [:div.Cell] (content v))) – DanLebrero Oct 09 '14 at 22:10
  • Ok, this should be it (html/defsnippet value-row3 (template-div) [:div.Row] [event-data] (html/clone-for [vs event-data] [:div.Cell] (html/clone-for [v vs] (html/content v)))) and then the call is like this (value-row3 (mapv vals (:event-data dummy-content))) – Vesna Oct 10 '14 at 14:32
  • this works fine as a snippet, but now I'm having trouble with calling a template, (html/deftemplate mshp (index) [cont] [:div.Title] (html/content (:title cont)) [:div.Heading] (html/content (for [c (keys (first (:event-data cont)))] (header-cell (name c)))) [:div.Row] (value-row (mapv vals (:event-data cont)))), and a call (print (apply str (mshp dummy-content))) gives me this error ClassCastException clojure.lang.LazySeq cannot be cast to clojure.lang.IFn net.cgrand.enlive-html/transform-loc (enlive_html.clj:426) – Vesna Oct 10 '14 at 15:23
  • this is template that works, (html/deftemplate mshp (index) [cont] [:div.Title] (html/content (:title cont)) [:div.Heading] (html/content (for [c (keys (first (:event-data cont)))] (header-cell (name c)))) [:div.Row] (html/content (value-row (mapv vals (:event-data cont))))), and a call to the template (print (apply str (mshp dummy-content))) – Vesna Oct 10 '14 at 15:51