根据jekyll中的for循环中的项目添加类元素
我有一个简单的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
获取数组大小的正确方法是
{%如果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/