13832

Clojure: enlive deftemplate can't use snippet

Question:

I'm trying to create template that produces table with some data in it. Data comes from a map defined in msh-contents.

(require '[net.cgrand.enlive-html :as html]) (def msh-contents {:title "Events mashup", :data-content [{:title "ICTM Study Group ", :url "http://some-url.com"} {:title "Volodja Balzalorsky - Hinko Haas", :url "http://some- other-url.com"} ]}) ;; define snippets based on some divs in the template (html/defsnippet header-cell (template-div) [:div.Heading :div.Cell][value] (html/content value)) (html/defsnippet value-cell (template-div) [:div.Row :div.Cell] [value] (html/content value)) ;; define a template (html/deftemplate mshp "index.html" [cont] [:div.Heading] (html/content (for [c (keys (first (:data-content cont)))] (header-cell (name c)))) [:div.Row] (html/content (map #(value-cell %) (for[e (:data-content cont)] (vals e)))))

So, callling the template in REPL

(mshp msh-contents)

Produces error: IllegalArgumentException ArityException Wrong number of args (0) passed to: PersistentArrayMap clojure.lang.AFn.throwArity (AFn.java:437), and its probably because snippets produces LazySeq.

Answer1:

This might be really dumb, but the code works perfectly fine for me. The only difference I made was represent your html as hiccup notation then converting to nodes which is equivalent to just giving it a html file(I do this because I still haven't figured out how exactly html-resource works and not sure where I can put the file). Working code below:

(require '[net.cgrand.enlive-html :as h]) (def msh-contents {:title "Events mashup", :data-content [{:title "ICTM Study Group ", :url "http://some-url.com"} {:title "Volodja Balzalorsky - Hinko Haas", :url "http://some- other-url.com"}]}) (defn template-div [] (h/html [:div {:class "Table"} [:div {:class "Title"} [:p "This is a Table"]] [:div {:class "Heading"} [:div {:class "Cell"} [:p "Heading 1"]]] [:div {:class "Row"} [:div {:class "Cell"} [:p "Row 1 Column 1"]]]])) (defn index [] (h/html [:html [:div {:class "Table"} [:div {:class "Title"} [:p "This is a Table"]] [:div {:class "Heading"} [:div {:class "Cell"} [:p "Heading 1"]]] [:div {:class "Row"} [:div {:class "Cell"} [:p "Row 1 Column 1"]]]]])) (h/defsnippet header-cell (template-div) [:div.Heading :div.Cell] [value] (h/content value)) (h/defsnippet value-cell (template-div) [:div.Row :div.Cell] [value] (h/content value)) (h/deftemplate mshp (index) [cont] [:div.Heading] (h/content (for [c (keys (first (:data-content cont)))] (header-cell (name c)))) [:div.Row] (h/content (map #(value-cell %) (for[e (:data-content cont)] (vals e)))))

Final Result:

(clojure.string/join (mshp msh-contents)) => "<html><div class=\"Table\"><div class=\"Title\">

This is a Table

</div><div class=\"Heading\"><div class=\"Cell\">title</div><div class=\"Cell\">url</div></div><div class=\"Row\"><div class=\"Cell\">ICTM Study Group http://some-url.com</div><div class=\"Cell\">Volodja Balzalorsky - Hinko Haashttp://some- other-url.com</div></div></div></html>"

Perhaps you're giving defsnippet a bad source when doing (template-div). defsnippet and deftemplate both take nodes. When you give them a file like "index.html", it converts the file into nodes.

Recommend

  • Why won't polymer's flex attribute/class just work?
  • Dynamic Sieve Algorithms for Prime Generation
  • Is it possible to add a text overlay to videos?
  • How to replace one set of color 'shades' with another set
  • How can I fill all space of a div 100% with 3 divs with the same size?
  • Optional spaces and multiple alternatives in wl-pprint-extras
  • Using let variables in a lambda in Scheme
  • cudaFree is not freeing memory
  • Should a C# struct have only read-only properties
  • QTP UFT Unable to find row count from table
  • Using multiple input pipelines in TensorFlow
  • Manipulating matrices in C through R
  • Specify HTTPS for custom WCF Binding
  • Pythons argparse default value doesn't work
  • What does the TypeScript “lib” option really do?
  • (Play 2.5) How do you define json format for type alias of an Option?
  • incomplete type 'struct' error in C
  • Python/Javascript: WYSIWYG html editor - Handle large documents fast and/or design theory
  • Swift Initialization Rule Confusion
  • KnockoutObservableArray with typed elements in TypeScript
  • Center align outputs in ipython notebook
  • Remove changes from one element when event occurs on another element?
  • Checking if an array in C is symmetric
  • saving file generated by TCPDF
  • How to view images from protected folder with php?
  • C: Incompatible pointer type initializing
  • custom UITableViewCell with image for highlighting
  • Textfile Structure (tables)
  • Use of this Javascript
  • Sencha Touch 2.0 Controller refs attribute not working?
  • C++ Partial template specialization - design simplification
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • What is the “return” in scheme?
  • Sending data from AppleScript to FileMaker records
  • vba code to select only visible cells in specific column except heading
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • How to get next/previous record number?
  • Python: how to group similar lists together in a list of lists?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy