Say I have only access to <article>
and child elements in my /public/articles.html
file.
...
<article class="clj-article">
<h1 class="clj-title"></h1>
<p class="clj-date"></p>
<ul>
<li class="clj-tag"></li>
</ul>
</article>
...
How can I iterate on <article>
without knowing about a parent element? (with a defsnippet
in a deftemplate
)
(def articles [{:title "title1"
:date "date1"
:tags ["tag1" "other tag1"]}
{:title "title2"
:date "date2"
:tags ["tag2" "other tag2"]}])
(e/defsnippet show-articles "public/articles.html" [:.clj-article] [articles]
[:.clj-article]
(e/clone-for [a articles]
[:.clj-title]
(e/content (:title a))
[:.clj-date]
(e/content (:date a))
[:.clj-tag]
(e/clone-for [t (:tags a)]
[:.clj-tag] (e/content t))))
My snippet give the output I need, and then I want to insert it in my template (as well as other snippets in other parts of articles.html) As I don't know about .clj-article
parent element, I've tried targeting it:
(e/deftemplate index "public/articles.html" []
[:.clj-article]
(e/append (show-articles articles)))
I get the output I wish, but wrapped in an other <article>
and children pattern.
<article class="clj-article">
<h1 class="clj-title"></h1>
<p class="clj-date"></p>
<ul class="clj-tags">
<li class="clj-tag"></li>
</ul>
<article class="clj-article">
<h1 class="clj-title">title1</h1>
<p class="clj-date">date1</p>
<ul class="clj-tags">
<li class="clj-tag">tag1</li>
<li class="clj-tag">other tag1</li>
</ul>
</article>
<article class="clj-article"> <h1 class="clj-title">title2</h1>
<p class="clj-date">date2</p>
<ul class="clj-tags">
<li class="clj-tag">tag2</li>
<li class="clj-tag">other tag2</li>
</ul>
</article>
</article>
How can I prevent that from happening?
Thanks!
Christophe Grand had the answer in his wonderful tutorial! https://github.com/cgrand/enlive/wiki/Table-and-Layout-Tutorial,-Part-3:-Simple-Transformations#whole-tag-transformations
Instead of appending ou prepending a snippet to a parent element, overwrite the actual element by using e/substitute
(e/deftemplate index "public/articles.html" []
[:.clj-article]
(e/substitute (show-articles articles)))