2014-07-13 32 views
0

我想創建一個模板,它產生一些數據在其中的表。數據來自在msh-contents中定義的映射。Clojure:活躍的deftemplate不能使用片段

(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))))) 

所以,在REPL

(mshp msh-contents) 

callling模板產生錯誤:拋出:IllegalArgumentException ArityException錯誤數args來傳遞到(0):PersistentArrayMap clojure.lang.AFn.throwArity(AFn.java:437 ),可能是因爲片段產生了LazySeq。

回答

1

這可能真的很愚蠢,但代碼對我來說工作得很好。我所做的唯一區別是表示你的html爲打嗝符號,然後轉換爲節點,相當於只是給它一個html文件(我這樣做是因爲我仍然沒有想出html-resource究竟是如何工作的,並且不確定我可以把它放在哪裏文件)。下面的工作代碼:

(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))))) 

最終結果:

(clojure.string/join (mshp msh-contents)) 
=> "<html><div class=\"Table\"><div class=\"Title\"><p>This is a Table</p></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>" 

也許你正在做(template-div)時給予defsnippet不好的來源。 defsnippetdeftemplate都取節點。當你給他們一個像"index.html"這樣的文件時,它將文件轉換成節點。

+0

謝謝,我會研究這一點。不確定defsnippet不能正常工作,因爲我自己嘗試了,而且很好。我必須嘗試一下,所以它不會返回懶惰的seq,而只是字符串。 – Vesna

+0

優秀,我愛打嗝。這是html資源的問題,所以打嗝的解決方案要好得多。 – Vesna