Last Updated: February 25, 2016
·
953
· ddeaguiar

Test your enlive transformations in the repl

You can experiment with your enlive transformations in the repl using the sniptest macro that ships with enlive.

Here's a contrived example:

example.core> (html/sniptest "<p></p>" (html/content "test"))
;; "<p>test</p>" 

For a more interesting example, let's assume we have a template example/views/base.html that contains the following html:

<nav id="main">
     <ul>
         <li><a href="#first">First</a></li>
         <li><a href="#second">Second</a></li>
         <li><a href="#third">Third</a></li>
      </ul>
</nav>

Consider a snippet, navigation, that uses this template to produce an li element containing a link.

(html/defsnippet navigation "example/views/base.html" [:nav#main :ul [:li html/first-of-type]]
[nav]
[:a] (html/do-> (html/set-attr :href (:uri nav))
              (html/content (:content nav))))

We can map this snippet across link data to generate a collection of li nodes which can then be passed to sniptest for review. For this example, nav-data contains our sample link data.

example.core> nav-data
;; ({:uri "#services", :content "Services"} {:uri "#people", :content "People"} {:uri "#contact", :content "Contact"})

example.core> (html/sniptest "<ul></ul>" (html/content (map navigation nav-data)))
;;"<ul><li><a href=\"#services\">Services</a></li><li><a href=\"#people\">People</a></li><li><a href=\"#contact\">Contact</a></li></ul>"