【问题标题】:Clojure enlive: the function html-content can't operate a function with an argument (?)Clojure enlive:函数 html-content 不能操作带参数的函数(?)
【发布时间】:2014-11-13 19:37:20
【问题描述】:

所以我用deftemplate做了一个简单的html模板,

(html/deftemplate header "selmer/header.html"
  [])    

(html/deftemplate footer "selmer/footer.html"
  [])

(html/deftemplate blogp "selmer/blogpage.html"
  [id]
  [:bjudul] (html/content (:title (db/finddata id)))
  [:bisi] (html/content (:content (db/finddata id))))

(注意:db/finddata id 是一个函数,它接受一个数字作为 data Id,并通过某个 data id 从我的数据库中返回一个数据映射, 例如,如果我输入

(db/finddata 1)   

它会产生这个

{:title "Wowtitle", :content "wowcontent", :id 1}

它是来自我的数据库的数据,id 为 1)

然后

(html/deftemplate layout "selmer/layout.html"
  [content contenttitle]
  [:title] (html/content contenttitle)
  [:header] (html/html-content (apply str (header)))
  [:pagecontents] (html/html-content (apply str (content)))
  [:footer] (html/html-content (apply str (footer))))

(defn createpage [pcontents tcontent]
  (apply str (layout pcontents tcontent)))

但是当我在 repl 中输入这个时

(createpage (blogp id) "Blog")

它会产生这个错误

ClassCastException clojure.lang.PersistentVector$ChunkedSeq cannot be cast to clojure.lang.IFn  zenblog.pageandctrl.pagelayout/fn--14851/fn--14853/fn--14854 (form-init2686073120612682758.clj:1)

它似乎与另一个 deftemplate 配合得很好,例如,如果我更改 blogp 代码

(html/deftemplate blogp "selmer/blogpage.html"
  []
  [:bjudul] (html/content (:Judul (db/findById **1**)))
  [:bisi] (html/content (:Isi (db/findById **1**))))

然后我在repl中输入了这个

(createpage blogp "Blog")

它工作得很好。任何想法为什么?

我是 clojure 的新手,我也是 enlive 的新手

【问题讨论】:

    标签: html clojure enlive


    【解决方案1】:

    您的layout 模板期望content 参数是一个函数,而当您执行(blogp id) 时,您实际上传递的是函数的结果,而不是函数本身。你可以做的是这样使用它:

    (createpage (partial blogp id) "Blog")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多