clojureenlive

Enlive - Wrap tags around HTML from file


So I have the following HTML in logout.html:

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

It looks like I need some function to read logout.html as enlive nodes (At least I think wrap takes nodes; I'm not actually sure).

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

Solution

  • I ended up having to modify overthink's answer to get it working.

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

    How wrap works is it wraps selected elements in a given tag. So in this case, #log_out is selected and is wrapped with the li tag.

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

    It's definitely not as clean as I'd like, but it works.