Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use enlive's clone-for with an HTML template to remove dummy child elements

See this google groups topic

Several people have expressed confusion with using clone-for to insert a list of dynamically generated elements into a template which includes several dummy elements. For example, maybe the template looks like this,

<ul>
    <li>foo</li>
    <li>bar</li>
    <li>baz</li>
</ul>

And we want to generate HTML like this,

<ul>
    <li>real</li>
    <li>data</li>
    <li>here</li>
    <li>wurdz</li>
</ul>

The naive thing to try is something like

(defsnippet my-snippet "my-template.html" [:ul] [items] 
    [[:li first-of-type]] (clone-for [ii items]
                             [:li] (content ii)))

But that leaves the 2nd through nth elements with the dummy data. How can we simply remove all the dummy elements and replace with real one?

like image 203
Johnny Brown Avatar asked Jan 12 '23 06:01

Johnny Brown


1 Answers

The solution I've been using is to do something like,

(defsnippet my-snippet "my-template.html" [:ul] [items]
  [[:li (html/but html/first-of-type)]] nil
  [[:li html/first-of-type]] (html/clone-for [ii items] ...))

Which deletes all the dummy nodes, and inserts new ones with my content.

like image 137
Johnny Brown Avatar answered Jan 31 '23 04:01

Johnny Brown