4

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?

Johnny Brown
  • 1,000
  • 11
  • 18

2 Answers2

5

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.

Johnny Brown
  • 1,000
  • 11
  • 18
4

I think that a different approach could be something like this:

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

(deftemplate my-template "my-template.html"
  [items]
    [:ul] (content (my-snippet items)))

First, you define snippets with the specific elements that you want to clone with real data, then you can define a template and replace the contents of the elements where you want to put the snippets. This way you don't have to delete the dummy nodes.

izaban
  • 1,013
  • 7
  • 15