根据jekyll中的for循环中的项目添加类元素

发布于 2025-01-21 21:36:46 字数 472 浏览 1 评论 0原文

我有一个简单的for loop,它从我的json文件中从我的_data文件夹中拉出。我希望能够将类添加到所有h1s的情况下,如果有3个以上的标题,并且如果少于3个标题,则删除该类。我尝试使用有条件的逻辑,但似乎并没有将类添加到所有内容中。

{% for book in site.data.books limit: 6 %}
    If > 3
        <h1 class="BookTitle {% if book.title > 3 %}BookTitleAlt{% endif %}>{{ book.title }}</h1>
    If < 3
        <h1 class="BookTitle">{{ book.title }}</h1>
{% endfor %}

I have a simple for loop that pulls from a JSON file in my _data folder. I want to be able to add a class to all the H1s if there are more than 3 titles and remove the class if there are less than 3 titles. I tried using conditional logic but doesn't seem to add the class to everything.

{% for book in site.data.books limit: 6 %}
    If > 3
        <h1 class="BookTitle {% if book.title > 3 %}BookTitleAlt{% endif %}>{{ book.title }}</h1>
    If < 3
        <h1 class="BookTitle">{{ book.title }}</h1>
{% endfor %}

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

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

发布评论

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

评论(1

败给现实 2025-01-28 21:36:46

获取数组大小的正确方法是{%如果book.size&gt; 3%},但我有一种感觉,您想要这个而是

{%如果site.data.data.books.size&gt; 3%}

参考: https://shopify.github.io/liquid/过滤器/尺寸/

The correct way to get the size of an array is this {% if book.size > 3 %} but I have a feeling you want this instead

{% if site.data.books.size > 3 %}

Reference: https://shopify.github.io/liquid/filters/size/

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