我是 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) [["\\\\\\{\\\\\\{" "{{"] ["\\\\\\}\\\\\\}" "}}"]])))
您正在调用采用 [模板数据部分]
的 3 元重载,因此带有 :news
键的第二个 map 被视为 部分
由clostache制作。您想要调用仅需要 [模板数据]
的 2 元版本,并传递带有键 :news
和 :sports
的一张 map 。
关于mysql - clostache/render 函数中是否有多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31858771/