Question

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"))))
Was it helpful?

Solution 2

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.

OTHER TIPS

Not sure if this is the best way, but you could define the contents of logout.html as an enlive snippet. A snippet is like a template, but returns nodes, and can also selectively grab parts of the file given a selector (in the example below, the selector is :#log_out, meaning the form element with id="log_out").

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

Then something like:

(html/defsnippet nav "templates/nav.html" [:ul]
  []
  [:ul] (html/append
          ((html/wrap :li) (logout-form))))) ;; untested! ymmv
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top