Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9166019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:03:33+00:00 2026-06-17T15:03:33+00:00

I’m a Rails dev getting my feet wet in Clojure. I’m trying to do

  • 0

I’m a Rails dev getting my feet wet in Clojure. I’m trying to do something which was very simple with ERB but I can’t for the life of me figure it out in enlive.

Say I have a simple layout file for a website in layout.html:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>

And I have these snippets, for instance, header.html and footer.html and this simple route.

(deftemplate layout "layout.html" [])

(defroutes home-routes
  (GET "/" [] layout))

How can I make it so whenever a request goes to “/” it transforms the layout and inserts the header and footer snippets into it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:03:35+00:00Added an answer on June 17, 2026 at 3:03 pm

    defsnippet only matches a specific part of your html (which is why it takes a selector as an argument), and transforms it. deftemplate takes the entire html, and transforms it. Also, defsnippet returns a Clojure data structure while deftemplates returns a vector of strings, so a defsnippet is usually used within a deftemplate.

    To give you an idea of what the data returned by a snippet (or selector) look like:

    (enlive/html-snippet "<div id='foo'><p>Hello there</p></div>")
    ;=({:tag :div, :attrs {:id "foo"}, :content ({:tag :p, :attrs nil, :content ("Hello there")})})
    

    In your case you want something like:

    header.html:

    <div id="my-header-root">
    ...
    </div>
    

    Clojure code:

    (enlive/defsnippet header "path/to/header.html" [:#my-header-root] []
    identity)
    
    (enlive/defsnippet footer "path/to/footer.html" [enlive/root] []
    identity)
    
    (enlive/deftemplate layout "layout.html" [header footer]
    [:head] (enlive/content header)
    [:body] (enlive/append footer))
    
    (defroutes home-routes
      (GET "/" [] (layout (header) (footer))
    

    The identity function used in the snippets returns it’s argument, which in this case is the data structure selected by the :#my-header-root selector (we don’t do any transformation). If you want to include everything in i.e head.html you can use the root selector-step that comes with enlive.

    You can view the html generated from a defsnippet using something like this:

    (print (apply str (enlive/emit* (my-snippet))))
    

    I also recommend the tutorial: https://github.com/swannodette/enlive-tutorial/
    and the one by Brian Marick for some more details of how the defsnippet and deftemplate macros work.

    Last tip, you can experiment with selectors and transformations using the sniptest macro that comes with enlive:

    (enlive/sniptest "<p>Replace me</p>"
    [:p] (enlive/content "Hello world!"))
    ;= "<p>Hello world!</p>"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.