Created
September 8, 2012 06:13
-
-
Save edtsech/3672297 to your computer and use it in GitHub Desktop.
Enlive templating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use 'net.cgrand.enlive-html) | |
; Define our posts, it can be response from the database. | |
(def posts [{:title "First post" | |
:content "Content of my first post." | |
:comments [{ | |
:user "edtsech" | |
:text "boom"} | |
{:user "anonymous" | |
:text "blah-blah-blah"}]} | |
{:title "Second post" | |
:content "Content of my second post."}]) | |
; Define Enlive template. | |
(deftemplate t "input.html" [] | |
; Clone article tag for each post | |
[:article] (clone-for [post posts] | |
; Replace a fake content of the header with the actual title of the post. | |
[:h2] | |
(content (:title post)) | |
; Replace a fake content of the paragraph with the actual content of the post. | |
[:p] | |
(content (:content post)) | |
; Clone a li tag for each post's comment. | |
[:ul.comments :li] | |
(clone-for [comment (:comments post)] | |
; Replace a fake user name | |
[:span.user] | |
(content (:user comment)) | |
; Replace a fake comment content | |
[:span.comment] | |
(content (:text comment))))) | |
(apply str (t)) ; => will return output.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Blog</title> | |
</head> | |
<body> | |
<h1>Blog</h1> | |
<article> | |
<h2>Post title</h2> | |
<p>Post content...</p> | |
<ul class="comments"> | |
<li> | |
<span class="user">guest</span> | |
<span class="comment">text text<span> | |
</li> | |
</ul> | |
</article> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Blog</title> | |
</head> | |
<body> | |
<h1>Blog</h1> | |
<article> | |
<h2>First post</h2> | |
<p>Content of my first post.</p> | |
<ul class="comments"> | |
<li> | |
<span class="user">edtsech</span> | |
<span class="comment">boom</span></li><li> | |
<span class="user">anonymous</span> | |
<span class="comment">blah-blah-blah</span></li> | |
</ul> | |
</article><article> | |
<h2>Second post</h2> | |
<p>Content of my second post.</p> | |
<ul class="comments"> | |
</ul> | |
</article> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment