无法在 `nil` 上使用 concat
Unable to use concat on `nil`
我正在尝试为我的 Jekyll 站点构建一个相关的 post 包含文件。该站点基于成员、景点和公园(每个都是集合)的概念。每个 post 都有多对多的关系。我正在尝试建立一个包含每个页面属性(成员、景点和公园)的组合数组,循环遍历该数组并找到具有相同数量标签的 posts。
这很简单,但我被一个部分卡住了,并非所有 post 都有成员、景点和公园字段,因此它们返回 nil
,但 concat 过滤器需要一个数组。我试图将变量默认设置为 []
,但它总是被设置为 nil
。有什么想法吗?
代码如下:
<ul class="row">
{% assign pageTags = [] %}{% if page.tags.first %}{% assign pageTags = page.tags %}{% endif %}
{% assign pageAttractions = [] %}{% if page.attractions.first %}{% assign pageAttractions = page.attractions %}{% endif %}
{% assign pageMembers = [] %}{% if page.members.first %}{% assign pageMembers = page.members %}{% endif %}
{% assign pageParks = [] %}{% if page.parks.first %}{% assign pageParks = page.parks %}{% endif %}
{% assign pageTagList = pageTags | concat: pageAttractions | concat: pageMembers | concat: pageParks %}
{% for post in site.documents %}
{% assign sameTagCount = 0 %}
{% assign commonTags = '' %}
{% assign postTags = [] %}{% if post.tags %}{% assign postTags = post.tags %}{% endif %}
{% assign postAttractions = [] %}{% if post.attractions %}{% assign postAttractions = post.attractions %}{% endif %}
{% assign postMembers = [] %}{% if post.members %}{% assign postMembers = post.members %}{% endif %}
{% assign postParks = [] %}{% if post.parks %}{% assign postParks = post.parks %}{% endif %}
{% assign postTageList = postTags | concat: postAttractions | concat: postMembers | concat postParks %}
{% if post.hidden == true %}
{% break %}
{% endif %}
{% for tag in postTageList %}
{% if post.url != page.url %}
{% if pageTagList contains tag %}
{% assign sameTagCount = sameTagCount | plus: 1 %}
{% capture tagmarkup %} <span class="label label-default">{{ tag }}</span> {% endcapture %}
{% assign commonTags = commonTags | append: tagmarkup %}
{% endif %}
{% endif %}
{% endfor %}
{% if sameTagCount >= minCommonTags %}
<li class="col-lg-4 col-md-12">
<div class="main-image">
<a href="{{ post.url }}" class="image" style="background-image: url('{{ post.image }}');"></a>
</div>
<h5>{{ post.categories | first }}</h5>
<h3><a href="{{ post.url }}">{{ post.title | replace: 'Review', '' }}</a></h3>
<p>
{% if post.description %}
{{ post.description }}
{% else %}
{{ post.content | markdownify | strip_html | truncatewords: 20 }}
{% endif %}
</p>
<p>
<a href="{{ post.url }}" class="large">Read Article →</a>
</p>
</li>
{% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}
{% if maxRelatedCounter >= maxRelated %}
{% break %}
{% endif %}
{% endif %}
{% endfor %}
</ul>
您可以在此处查看存储库:https://github.com/dtsn/jungleskipper/blob/feature/members/_includes/related-posts.html
您应该查看 compact,它从数组中删除任何 nil 值。
这是 shopify 上文档的 link。
示例来自 Liquid 文档
输入:
{% assign site_categories = site.pages | map: "category" %}
{% for category in site_categories %}
- {{ category }}
{% endfor %}
输出:
- business
- celebrities
-
- lifestyle
- sports
-
- technology
输入:
{% assign site_categories = site.pages | map: "category" | compact %}
{% for category in site_categories %}
- {{ category }}
{% endfor %}
输出:
- business
- celebrities
- lifestyle
- sports
- technology
You cannot initialize arrays using only Liquid.
You can, however, use the split filter to break a string into an array of substrings.
我正在尝试为我的 Jekyll 站点构建一个相关的 post 包含文件。该站点基于成员、景点和公园(每个都是集合)的概念。每个 post 都有多对多的关系。我正在尝试建立一个包含每个页面属性(成员、景点和公园)的组合数组,循环遍历该数组并找到具有相同数量标签的 posts。
这很简单,但我被一个部分卡住了,并非所有 post 都有成员、景点和公园字段,因此它们返回 nil
,但 concat 过滤器需要一个数组。我试图将变量默认设置为 []
,但它总是被设置为 nil
。有什么想法吗?
代码如下:
<ul class="row">
{% assign pageTags = [] %}{% if page.tags.first %}{% assign pageTags = page.tags %}{% endif %}
{% assign pageAttractions = [] %}{% if page.attractions.first %}{% assign pageAttractions = page.attractions %}{% endif %}
{% assign pageMembers = [] %}{% if page.members.first %}{% assign pageMembers = page.members %}{% endif %}
{% assign pageParks = [] %}{% if page.parks.first %}{% assign pageParks = page.parks %}{% endif %}
{% assign pageTagList = pageTags | concat: pageAttractions | concat: pageMembers | concat: pageParks %}
{% for post in site.documents %}
{% assign sameTagCount = 0 %}
{% assign commonTags = '' %}
{% assign postTags = [] %}{% if post.tags %}{% assign postTags = post.tags %}{% endif %}
{% assign postAttractions = [] %}{% if post.attractions %}{% assign postAttractions = post.attractions %}{% endif %}
{% assign postMembers = [] %}{% if post.members %}{% assign postMembers = post.members %}{% endif %}
{% assign postParks = [] %}{% if post.parks %}{% assign postParks = post.parks %}{% endif %}
{% assign postTageList = postTags | concat: postAttractions | concat: postMembers | concat postParks %}
{% if post.hidden == true %}
{% break %}
{% endif %}
{% for tag in postTageList %}
{% if post.url != page.url %}
{% if pageTagList contains tag %}
{% assign sameTagCount = sameTagCount | plus: 1 %}
{% capture tagmarkup %} <span class="label label-default">{{ tag }}</span> {% endcapture %}
{% assign commonTags = commonTags | append: tagmarkup %}
{% endif %}
{% endif %}
{% endfor %}
{% if sameTagCount >= minCommonTags %}
<li class="col-lg-4 col-md-12">
<div class="main-image">
<a href="{{ post.url }}" class="image" style="background-image: url('{{ post.image }}');"></a>
</div>
<h5>{{ post.categories | first }}</h5>
<h3><a href="{{ post.url }}">{{ post.title | replace: 'Review', '' }}</a></h3>
<p>
{% if post.description %}
{{ post.description }}
{% else %}
{{ post.content | markdownify | strip_html | truncatewords: 20 }}
{% endif %}
</p>
<p>
<a href="{{ post.url }}" class="large">Read Article →</a>
</p>
</li>
{% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}
{% if maxRelatedCounter >= maxRelated %}
{% break %}
{% endif %}
{% endif %}
{% endfor %}
</ul>
您可以在此处查看存储库:https://github.com/dtsn/jungleskipper/blob/feature/members/_includes/related-posts.html
您应该查看 compact,它从数组中删除任何 nil 值。
这是 shopify 上文档的 link。
示例来自 Liquid 文档
输入:
{% assign site_categories = site.pages | map: "category" %}
{% for category in site_categories %}
- {{ category }}
{% endfor %}
输出:
- business
- celebrities
-
- lifestyle
- sports
-
- technology
输入:
{% assign site_categories = site.pages | map: "category" | compact %}
{% for category in site_categories %}
- {{ category }}
{% endfor %}
输出:
- business
- celebrities
- lifestyle
- sports
- technology
You cannot initialize arrays using only Liquid.
You can, however, use the split filter to break a string into an array of substrings.