0
votes

I am trying to build a related post include file for my Jekyll site. The site is based around the concept of members, attractions and parks (each as collections). Each post has a many to many relationships. I am trying to up a combined array of each of the page attributes (members, attractions and parks) loop through the array and find posts with a common number of tags.

It's quite simple but I am getting stuck with one section, not all the posts have members, attractions and parks fields so they are returning nil but the concat filter requires an array. I am trying to default the variable to an [] but it always gets set to nil. Any ideas?

Here's the code:

<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 &rarr;</a>
                    </p>
                </li>
            {% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}
            {% if maxRelatedCounter >= maxRelated %}
                {% break %}
            {% endif %}
        {% endif %}
    {% endfor %}
    </ul>

You can see the repo here: https://github.com/dtsn/jungleskipper/blob/feature/members/_includes/related-posts.html

2

2 Answers

0
votes

From the Liquid documentation:

You cannot initialize arrays using only Liquid.

You can, however, use the split filter to break a string into an array of substrings.

0
votes

You should look at compact which removes any nil values from an array.

Here is a link to the doc on shopify.

Example from Liquid documentation

Input:

{% assign site_categories = site.pages | map: "category" %}

{% for category in site_categories %}
- {{ category }}
{% endfor %}

Output:

- business
- celebrities
-
- lifestyle
- sports
-
- technology

With Compact

Input:

{% assign site_categories = site.pages | map: "category" | compact %}

{% for category in site_categories %}
- {{ category }}
{% endfor %}

Output:

- business
- celebrities
- lifestyle
- sports
- technology