我是Clojure的新手,我正在尝试制作一个页面,在该页面中您可以看到左侧表格中的所有新闻,而在页面右侧仅显示体育新闻。我试图向Clostache / render添加一个新参数:

(defn render-template [template-file params param]
  (clostache/render (read-template template-file) params param))

(defn welcome []
  (render-template "index" {:sports (model/justSports)} {:news (model/all)}))


model / all和model / justSports在哪里:

    (defn all []
  (j/query mysql-db
    (s/select * :news)))

(defn justSports []
  (j/query mysql-db
    (s/select * :news ["genre = ?" "sports"])))


新闻应该显示为:

<div style="background-color: #D3D3D3; width: 450px; height: 800px; position: absolute; right: 10px; margin-top: 10px; border-radius: 25px;">
       <sections>
         {{#sports}}
         <h2>{{title}}</h2>
         <p>{{text}}<p>
         {{/sports}}
       </sections>
       </div>

      <div class="container"  style="width: 500px; height: 800px; position: absolute; left: 20px;">
      <h1>Listing Posts</h1>
      <sections>
         {{#news}}
           <h2>{{title}}</h2>
           <p>{{text}}<p>
         {{/news}}
     </sections>
  </div>


但这是行不通的。它仅显示页面上第一个参数的数据。您如何看待我该如何做?

附言
不要介意丑陋的CSS,我会努力解决的:)

最佳答案

以下应使其起作用:

(defn render-template [template-file params]
  (clostache/render (read-template template-file) params))

(defn welcome []
  (render-template "index" {:sports (model/justSports)
                            :news (model/all)}))


render具有三个“ arities”:


(defn render
  "Renders the template with the data and, if supplied, partials."
  ([template]
     (render template {} {}))
  ([template data]
     (render template data {}))
  ([template data partials]
     (replace-all (render-template template data partials)
                  [["\\\\\\{\\\\\\{" "{{"]
                   ["\\\\\\}\\\\\\}" "}}"]])))



您正在调用需要[template data partials]的3-arity重载,因此clostache将带有:news键的第二个映射作为partials。您想调用仅用[template data]的2 arar版本,并用键:news:sports传递一张地图。

关于mysql - Clostache/Render函数中有多个参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31858771/

10-12 12:46