2

所以我有以下HTML logout.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"))))
4

2 回答 2

2

不确定这是否是最好的方法,但您可以将 的内容定义logout.html为实时片段。片段类似于模板,但返回节点,并且还可以在给定选择器的情况下选择性地抓取部分文件(在下面的示例中,选择器是:#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
于 2014-01-28T03:32:03.090 回答
1

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

(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))

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

于 2014-01-28T11:24:22.390 回答