【问题标题】:Enlive snippet giving me odd HTMLEnlive 片段给了我奇怪的 HTML
【发布时间】:2014-02-18 09:36:33
【问题描述】:

问题:Enlive sn-p 制作时髦的 HTML

问题的可视化参考:http://i.imgur.com/FIOzgZv.png

查看代码 sn-p 底部的奇怪 HTML

(ns notebook.handler
  (:require [compojure.core :refer :all]
            [compojure.handler :as handler]
            [compojure.route :as route]
            [net.cgrand.enlive-html :as html]))

(html/defsnippet nav "templates/nav.html" [:*]
      [])

(html/deftemplate home-page "templates/base.html"
  []
   [:body] (html/prepend (nav)))

(defroutes app-routes
  (GET "/" [] (home-page))
  (route/resources "/")
  (route/not-found "Not Found"))

(def app
   (handler/site app-routes))

base.html 的内容:

<html>
  <head>
    <link rel=stylesheet href="css/base.css">
  </head>
  <body>
  </body>
</html>

nav.html 的内容:

<nav>
  <ul>
    <li>FlatNotes</li>
  </ul>
</nav>

访问 localhost:3000 时的 HTML:

<html>
  <head>
    <link href="css/base.css" rel="stylesheet" />
  </head>
  <body><nav>
  <ul>
    <li>FlatNotes</li>
  </ul>

</nav><ul>
    <li>FlatNotes</li>
  </ul><li>FlatNotes</li>
  </body>

</html>

(reduce str (html/emit* (nav))) 显示奇怪的 HTML,这意味着问题出现在 defsnippet 之前的 deftemplate

"<nav>\n  <ul>\n\t<li>FlatNotes</li>\n  </ul>\n\n</nav><ul>\n\t<li>FlatNotes</li>\n  </ul><li>FlatNotes</li>"

也许我对 [:*] 所做的事情有误,或者存在根本性的误解,或者存在我不知道的问题。我已经将代码减少到尽可能少,所以 lolidk。

【问题讨论】:

    标签: clojure compojure enlive


    【解决方案1】:

    :* 代表通用选择器。它匹配 nav.html 中的每个元素 - navulli - 这意味着 nav sn-p 是:

    <nav>
      <ul>
        <li>FlatNotes</li>
      </ul>
    </nav>
    
    <ul>
      <li>FlatNotes</li>
    </ul>
    
    <li>FlatNotes</li>
    

    传递给 sn-p 定义的选择器应指向 sn-p 的单个顶级元素。如果您更改 :* 以匹配单个元素(即 :nav),它应该会为您提供所需的 sn-p。

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 2020-08-01
      • 1970-01-01
      • 2015-12-22
      相关资源
      最近更新 更多