mysql - clostache/render 函数中是否有多个参数?

标签 mysql clojure

我是 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/

相关文章:

java - 如何使用jdbc和sql查询编写条件?

mysql - sql_mode 没有更新,即使我在 my.cnf 中更改了它

sql - 如何通过聚合函数过滤MySQL SELECT?

java - Clojure/Java Mandelbrot 分形绘图

file-io - 在Clojure中的绝对位置打开文件

clojure - 寻求一些 Clojure 特殊形式和元数据的解释

clojure - 如何根据索引过滤序列中的元素

php - 如果在 SQL 中找到输入名称,则执行其他操作……使用 SQL 查询

php - 自动完成结果显示但找不到大写字母

haskell - TCO 在 Clojure 中优化了汉诺塔