Sir*_*rah 2 html templating liquid shopify
在shopify中,我使用液体模板来获取博客文章,这些博客文章通过其标签与产品相关,例如:
<ul>
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
<li><a href="{{ article.url }}"><p>{{ article.title }}</p></a></li>
{% endif %}
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
但是,如果没有相关信息,我想显示一条消息,例如“无相关信息!”。
我的问题是我该怎么做?我已经考虑过尝试将文章整理成一个数组并测试它是否为空,但是我很难找出这样做的方式。
谢谢!
尝试这样的事情:
{% assign related_posts = "" %}
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
{% capture post %}
<li><a href="{{ article.url }}"><p>{{ article.title }}</p></a></li>
{% endcapture %}
{% assign related_posts = related_posts | append:post %}
{% endif %}
{% endfor %}
{% if related_posts.size > 0 %}
<ul> {{ related_posts }} </ul>
{% else %}
No related posts!
{% endif %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3022 次 |
| 最近记录: |