如何停止具有“包含标签”的循环。在nunjucks中的某些项目中?

发布于 2025-02-11 00:51:21 字数 351 浏览 1 评论 0原文

我在我的博客网站上有此代码,但我希望循环停止,例如第二个项目/文章或第三个

<div class="container mar2ritlft">
    <div class="content3col">

        {%- for article in collections.article | reverse -%}
        {% include 'article-post.njk' %}
        {%- endfor -%}

    </div>
</div>

PS:“ collections.tricle”是元数据中具有“文章”标签的博客

I have this code on my blog site but I want the loop to stop in, for example, the second item/article or third

<div class="container mar2ritlft">
    <div class="content3col">

        {%- for article in collections.article | reverse -%}
        {% include 'article-post.njk' %}
        {%- endfor -%}

    </div>
</div>

Ps: the "collections.article" is blogs that have "article" tag in the metaData

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的新欢旧爱 2025-02-18 00:51:21

您可能需要通过允许循环的索引与实际文章有关系,重新考虑通过多个文章循环的方法。 是这样,您应该能够完成以下内容...

    {%- for articleTitle in collections.article | reverse -%}
      {% if loop.index > 2 %}
        {{articleTitle}} {# a collection of blog titles???#} 
        {% include "articles/article-post" + loop.index + ".njk" %}
      {% endif %}
    {%- endfor -%}

,您将拥有类似的东西。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

  • 在您的文章
  • 如果
  • 目录中
  • /
    ...ETC...

You may want to reconsider your approach on looping through multiple articles by allowing the index of your loop to have a relationship to the actual articles. If so, you should be able to pull off the following...

    {%- for articleTitle in collections.article | reverse -%}
      {% if loop.index > 2 %}
        {{articleTitle}} {# a collection of blog titles???#} 
        {% include "articles/article-post" + loop.index + ".njk" %}
      {% endif %}
    {%- endfor -%}

In your articles/ directory, you would have something like...

  • article-post1.njk
  • article-post2.njk
  • article-post3.njk
  • article-post4.njk
    ...etc...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文