0

假设我只能访问文件<article>中的子元素/public/articles.html

   ...
   <article class="clj-article">
        <h1 class="clj-title"></h1>
        <p class="clj-date"></p>
        <ul>
            <li class="clj-tag"></li>
        </ul>
    </article>
    ...

如何在<article>不知道父元素的情况下进行迭代?(defsnippet在 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))))

我的片段提供了我需要的输出,然后我想将它插入到我的模板中(以及articles.html 其他部分中的其他片段)由于我不了解.clj-article父元素,我尝试定位它:

(e/deftemplate index "public/articles.html" []
  [:.clj-article]
       (e/append (show-articles articles)))

我得到了我想要的输出,但是包裹在另一个<article>和子模式中。

<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>

我怎样才能防止这种情况发生?

谢谢!

4

1 回答 1

0

Christophe Grand 在他的精彩教程中给出了答案! https://github.com/cgrand/enlive/wiki/Table-and-Layout-Tutorial,-Part-3:-Simple-Transformations#whole-tag-transformations

而不是附加 ou 将片段添加到父元素,而是使用覆盖实际元素e/substitute

(e/deftemplate index "public/articles.html" []
  [:.clj-article]
       (e/substitute (show-articles articles)))
于 2014-08-08T02:08:36.313 回答