【问题标题】:Enlive - Wrap tags around HTML from fileEnlive - 将标签包裹在文件中的 HTML 周围
【发布时间】:2014-01-29 06:28:24
【问题描述】:

所以我在logout.html 中有以下 HTML:

<form id="log_out" name="log_out" action="/log_out" method="post">
  <input type="submit"
         value="Log Out!">
  </input>
</form>

看起来我需要一些函数来读取logout.html 作为活跃节点(至少我认为wrap 需要节点;我实际上不确定)。

(html/defsnippet nav "templates/nav.html" [:ul]
      []
      [:ul] (html/append
                  (html/wrap :li (html/SOME-FUNCTION-IDK "templates/logout.html"))))

【问题讨论】:

    标签: clojure enlive


    【解决方案1】:

    不确定这是否是最好的方法,但您可以将logout.html 的内容定义为enlive sn-p。 sn-p 类似于模板,但返回节点,并且还可以在给定选择器的情况下选择性地抓取文件的部分内容(在下面的示例中,选择器是:#log_out,表示 id="log_out" 的表单元素)。

    (html/defsnippet logout-form "templates/logout.html" [:#log_out] 
      [])
    

    然后是这样的:

    (html/defsnippet nav "templates/nav.html" [:ul]
      []
      [:ul] (html/append
              ((html/wrap :li) (logout-form))))) ;; untested! ymmv
    

    【讨论】:

      【解决方案2】:

      我最终不得不修改过度思考的答案以使其正常工作。

      (defn extract-body
        "Enlive uses TagSoup to parse HTML. Because it assumes that it's dealing with
         something potentially toxic, and tries to detoxify it, it adds <head> and
         <body> tags around any HTML you give it. So the DOM returned by html-resource
         has these extra tags which end up wrapping the content in the middle of our
         webpage. We need to strip these extra tags out."
        [html]
        (html/at html [#{:html :body}] html/unwrap))
      
      (html/defsnippet logout "templates/logout.html" [html/root] [])
      

      wrap 的工作原理是将选定元素包装在给定标签中。所以在这种情况下,#log_out 被选中并用li 标签包裹。

      (html/defsnippet nav "templates/nav.html" [html/root]
            []
            [:ul] (html/append (extract-body (logout)))
            [:#log_out] (html/wrap :li))
      

      它绝对没有我想要的那么干净,但它确实有效。

      【讨论】:

      • 您可以通过更改您的 defsn-p 来避免提取体黑客攻击,如下所示:(html/defsnippet logout "templates/logout.html" [:#log_out] [])。即从 logout.html 文件中按 id 选择表单。
      • 是的,看起来这样可以避免extract-body hack。
      猜你喜欢
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 2020-06-28
      • 2012-11-17
      相关资源
      最近更新 更多