GetStream - Group likes together, but make others behave like non-aggregated timeline - laravel-5.4

I need to group all likes so that it will act similar to LinkedIn likes (Imonoid, Stackoverflower, and 2 others liked a post) but would want other activities to show as single (non grouped) similar to a non-aggregated timeline feed.
{% if verb.infinitive == 'like' %}
{{ target }}{{ verb.id }}{{ time.strftime('%j') }}
{% elif verb.infinitive == 'articlelike' %}
{{ target }}{{ verb.id }}{{ time.strftime('%j') }}
{% elif verb.infinitive == 'company_post_like' %}
{{ target }}{{ verb.id }}{{ time.strftime('%j') }}
{% elif verb.infinitive == 'company_post_c_like' %}
{{ target }}{{ verb.id }}{{ time.strftime('%j') }}
{% elif verb.infinitive == 'like_post_comment' %}
{{ target }}{{ verb.id }}{{ time.strftime('%j') }}
{% else %}
{{ verb.id }}_{{ time.strftime("%Y-%m-%d") }}
{% endif %}

Change your else condition to something like the following and you should be all set:
{% else %}
{{ id }}
{% endif %}
This will have the effect of taking every other activity and putting it into its own group, effectively isolating the activity. The JSON payload won't look the same as your flat feed, but should still be manageable.

Related

JSONIFY Filter Odd Behavior in Jekyll

Goal
Display JSON similar to this from links that have the same category minus the current page.
"relatedLink": [ "https://example.com/category-slug/page-title.html", "https://example.com/category-slug/page-title.html", "https://example.com/category-slug/page-title.html" ],
Code
{% capture results %}[
{% for category in page.categories %}
{% for post in site.categories[category] %}
{% if post.url != page.url %}
{{ post.url | relative_url | prepend: site.url | replace:" ", "," | jsonify }}{% endif %}{% if forloop.last %}]{% else %}{% endif %}
{% endfor %}{% endfor %}{% endcapture %}
"relatedLink": {{ results }},
Results Error in JSON-LD
"relatedLink": [ "https://example.com/category-slug/page-title.html" "https://example.com/category-slug/page-title.html" "https://example.com/category-slug/page-title.html" ],
jsonify did not comma separate the values in the array.
I think this is expected behavior. You're calling jsonify on post.url. jsonify converts Hashs or Arrays to JSON, not strings.
The best way to get what you want is probably this:
{% capture results %}[
{% for category in page.categories %}
{% for post in site.categories[category] %}
{% if post.url != page.url %}
"{{ post.url | relative_url | prepend: site.url }}"{% unless forloop.last %},{% endunless %}
{% endif %}
{% endfor %}
{% endfor %}]
{% endcapture %}
"relatedLink": {{ results }}

Picture slider on Mobile

I am working on a Shopify store, and I want to display multiple product images depending on what options are selected. This is the code I am using, which works perfectly on desktop, but messed up the way pictures are displayed on slider in mobile:
{% assign wanted_alts = product.selected_or_first_available_variant.options %}
<ul class="product-single__thumbnails product-single__thumbnails-{{ section.id }}" data-slider-container>
{% if enable_thumbnail_slides == true %}
<div class="product-single__thumbnails-slider-track" data-slider-track>
{% endif %}
{% for media in product.media %}
{% if wanted_alts contains media.alt or media.alt == "All" %}
<li class="product-single__thumbnails-item product-single__thumbnails-item--{{ section.settings.media_size }} {% if enable_thumbnail_slides == true %} product-single__thumbnails-item-slide{% endif %} js"{% if enable_thumbnail_slides == true %} data-slider-slide-index="{{ forloop.index0 }}" data-slider-item{% endif %}>
<a href="{{ media.preview_image | img_url: product_image_zoom_size, scale: product_image_scale }}"
class="text-link product-single__thumbnail product-single__thumbnail--{{ section.id }}"
data-thumbnail-id="{{ section.id }}-{{ media.id }}"
{% if enable_thumbnail_slides == true %} data-slider-item-link{% endif %}
{% if enable_image_zoom %}data-zoom="{{ media.preview_image | img_url: product_image_zoom_size, scale: product_image_scale }}"{% endif %}>
{%- capture thumbnailAlt -%}
{%- if media.media_type == 'video' or media.media_type == 'external_video' -%}
{{ 'sections.featured_product.video_thumbnail_alt' | t: imageAlt: media.alt | escape }}
{%- elsif media.media_type == 'model' -%}
{{ 'sections.featured_product.model_thumbnail_alt' | t: imageAlt: media.alt | escape }}
{%- else -%}
{{ 'sections.featured_product.gallery_thumbnail_alt' | t: imageAlt: media.alt | escape }}
{%- endif -%}
{%- endcapture -%}
<img class="product-single__thumbnail-image" src="{{ media.preview_image | img_url: '110x110', scale: 2 }}" alt="{{ thumbnailAlt }}">
{%- if media.media_type == 'video' or media.media_type =='external_video' -%}
<div class="product-single__thumbnail-badge">
{% include 'icon-video-badge-full-color' %}
</div>
{%- endif -%}
{%- if media.media_type == 'model' -%}
<div class="product-single__thumbnail-badge">
{% include 'icon-3d-badge-full-color' %}
</div>
{%- endif -%}
</a>
</li>
{% endif %}
{% endfor %}
{% if enable_thumbnail_slides == true %}
</div>
{% endif %}
</ul>
The only things I changed are:
Included {% assign wanted_alts = product.selected_or_first_available_variant.options %} before the images list to find out what options I want to display.
Wrap each <li> item in a if statement that tests if I want to show that image or not (based on the alt_text):
{% for media in product.media %}
{% if wanted_alts contains media.alt or media.alt == "All" %} //Line I added
<li>
...
</li>
{% endif %}//Line I added
{% endfor %}
This is my website.
Now my thumbnails are messed up on mobile, but they look exactly like I wanted on desktop.
Can someone help me to figure this out?
This is how it looks on mobile now:
This is how it looked before I changed the code (and the way I want it to be):

Liquid/Jekyll: How to check for no posts when one has two or more conditions?

I am having trouble understanding how I might show a "no posts exist" message for a particular conditional statement with two variables.
In this example, let's say I have a collection of "animals" - on a particular page, I'd like a section that displays "primates that are herbivores":
{% for animal in site.animal %}
{% if animal.species == "primate" and animal.type == "herbivore" %}
{{ animal.content }}
{% endif %}
{% endfor %}
What I'd like to do is something like this (pseudocode):
{% if POSTS_EXIST_FOR_THIS_COMBO: (animal.species == "primate" and animal.type == "herbivore") %}
{% for animal in site.animal %}
{% if animal.species == "primate" and animal.type == "herbivore" %}
{{ animal.content }}
{% endif %}
{% endfor %}
{% else %}
There are no posts for this category.
{% endif %}
Note: This differs from examples like this, because I have two parameters to check. Can someone offer a suggestion about the syntax?
I think you can do the following where you at first filter all by species=primate from site.animal and then filter by type=herbivore from that pool and then check if the result exists.
{% assign animals = site.animal | where:"species","primate" | where:"type","herbivore" %}
{% if animals %}
{% for animal in animals %}
{{ animal.content }}
{% endfor %}
{% endif %}
Hope this helps.

Single Index Blog Post for Multiple Series Posts in Jekyll

I have several blog posts that fall under one umbrella blog post. For example, I have several posts about SQL Zoo tutorials, but I want to be able to link them all up to one "umbrella" post so that I only have one SQL Zoo post on the index page of my blog. I got this idea from: https://codeasashu.github.io/implement-series-post-jekyll-part-2/ and tried to follow the instructions, but now my series post does not show up on my index page. I have the following code in a file called post-series.html located in my _includes folder:
{% assign seriesarray = '|' | split : '|' %}
{% assign seriestitle = '' %}
{% assign serieslabel = '' %}
{% assign sortedposts = (site.posts | sort: 'date') %}
{% for post in sortedposts %}
{% if post.series and page.series_slug != nil and post.series == page.series_slug %}
{% capture postitem %} <li> {{ post.title }} </li> {% endcapture %}
{% assign seriesarray = seriesarray | push: postitem %}
{% assign seriestitle = 'Posts in this series' %}
{% assign serieslabel = 'Series Post' %}
{% elsif post.series != nil and page.series != nil and page.series == post.series %}
{% assign pageurl = page.url | split:'/' | last %}
{% assign posturl = post.url | split:'/' | last %}
{% if pageurl != posturl %}
{% capture postitem %} <li> {{ post.title }} </li> {% endcapture %}
{% else %}
{% capture postitem %} <li> {{ post.title }} </li> {% endcapture %}
{% endif %}
{% assign seriesarray = seriesarray | push: postitem %}
{% endif %}
{% if post.series_slug != nil and page.series != nil and page.series == post.series_slug %}
{% capture series_title %} {{ post.title }} {% endcapture %}
{% assign seriestitle = 'This posts is part of series - ' | append: series_title %}
{% assign serieslabel = 'This posts is part of series - ' | append: series_title %}
{% endif %}
{% endfor %}
{% capture serieslayout %}
{% if seriesarray.size > 0 %}
<hr />
<div class="panel">
<div class="panel-body">
<h4> {{ seriestitle }} </h4>
<ul id="post-series-list">
{% endif %}
{% for post in seriesarray %} {{ post }} {% endfor %}
{% if seriesarray.size > 0 %} </ul> </div> </div> {% endif %}
{% endcapture %}
and the following code from my index.html file in the root of my directory:
---
layout: index
---
<div id="home">
<h1>{{ site.title }}</h1>
<hr />
<ol class="posts">
{% for post in paginator.posts %}
{% assign seriesPost = nil %}
{% if post.series == nil %}
{% if post.series_slug != nil %} {% assign seriesPost = '(Series)' %} {% endif %}
<li class="post-listing">
<img class="post__image" src="/static/img/{{ post.cover_image}}" alt="{{ post.cover_alt }}" />
<div class="post__text">
<a class="post__title" href="{{ post.url }}">{{ post.title }}</a><br>
<span>
{{ post.date | date_to_string }} •
{% assign words = post.content | number_of_words %}
{% if words < 360 %}
1 min read
{% else %}
{{ words | divided_by:180 }} min read
{% endif %}
</span>
{{ post.excerpt }}
</div>
</li>
{% endif %}
{% endfor %}
</ol>
<!-- <div class="sidebar-right sidebar"></div> -->
<!-- <ul>
{% for post in paginator.posts %}
<li>
{{ post.title }}
{{ post.excerpt }}
</li>
{% endfor %}
</ul> -->
<!-- Pagination links -->
{% if paginator.total_pages > 1 %}
<ul class="pagination pagination-sm">
{% if paginator.previous_page %}
<li>«</li>
{% else %}
<li class="disabled"><span aria-hidden="true">«</span></li>
{% endif %}
<li>First</li>
{% for page in (1..paginator.total_pages) %}
{% if page == paginator.page %}
<li class="active"><a>{{ page }}<span class="sr-only">(current)</span></a></li>
{% elsif page == 1 %}
<li>{{ page }}</li>
{% else %}
<li>{{ page }}</li>
{% endif %}
{% endfor %}
<li>Last</li>
{% if paginator.next_page %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
</div><!-- end #home -->
My full repo can be found here: https://github.com/thedatasleuth/thedatasleuth.github.io
In your index.html, {% if post.series == nil %} simply bares posts containing a series: someserie front matter variable to be printed.
For the second problem (note that on SO, you are supposed to ask one question at a time) :
Umbrella post always have series_slug: "My serie slug" in front
matter.
Serie's posts always have series: "My serie slug" in front
matter, and this must be strictly equal to umbrella page series_slug. (eg : you have a post with published: false and series: "SQL Zoology" that will not appear in SQL Zoo serie if you publish it.)
In _layouts/post.html remove {% include series.html %}.
In _includes/post-series.html replace all your code by the following :
{% comment %} #### On an umbrella page {% endcomment %}
{% if page.series_slug != nil %}
{% assign key = page.series_slug %}
{% assign title = page.title %}
{% assign url = page.url %}
{% assign sentence = "All posts in this serie :" %}
{% endif %}
{% comment %} #### On a serie page {% endcomment %}
{% if page.series != nil %}
{% assign key = page.series %}
{% assign umbrella_page = site.posts | where: 'series_slug', key | first %}
{% assign title = umbrella_page.title %}
{% assign url = umbrella_page.url %}
{% assign series_posts = site.posts | where: "series", key %}
{% for post in series_posts %}
{% if post.url == page.url %}
{% assign idx = forloop.index %}
{% endif %}
{% endfor %}
{% capture sentence %}
This article is <strong>Part {{ idx }}</strong> in a <strong>{{ series_posts.size }}-Part</strong> in {{ title }} serie
{% endcapture %}
{% endif %}
{% if page.series_slug != nil or page.series != nil %}
{% assign series_posts = site.posts | where: "series", key %}
<hr />
<div class="panel">
<div class="panel-body">
{% if page.series_slug != nil %}
{% assign key = page.series_slug %}
{% assign title = page.title %}
{% assign url = page.url %}
{% endif %}
<h4>{{ sentence }}</h4>
<ul id="post-series-list">
{% for post in series_posts %}
<li>
{% if page.url == post.url %}
This post : {{ post.title }} - part {{ forloop.index }}
{% else %}
{{ post.title }} - part {{ forloop.index }}
{% endif %}
</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
The problem you are facing
You forgot to add the series and series_slug YML variables. That is why it is not working in your case. You seem to not fully understand how the code works, probably due to the complexity of the solution. Therefore, I added another (much simpler) solution, that might fit your needs equally well.
A better/simpler solution
Simply add a YML variable called group: groupname to each post you want in a group. Do NOT skip any of the posts during pagination. Next, list the posts with the same group on the footer of each post (in your page/post layout) with the code below. Finally, add 'part 1', 'part 2', etc. to the post names in the series.
{% if post.group != nil %}
<ul>
{% for post in site.posts %}
{% if post.group == page.group %}
<li>{{ post.title }}</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
PS. If you really want to skip some posts in the pagination, I would create an universal solution. Add a skip_pagination boolean to the Front Matter (YML) and see if it is true in the pagination loop. This enables you to skip any post in the paginator.

Liquid + Jekyll, "unless" not working inside "for" loop

I'm making JSON given a set of pages. I want to skip any pages without titles, and the last element can not have a comma after it, that's bad JSON. Tried different variations, here is an example:
---
---
[
{% for page in site.pages %}
{% unless page.title %}
{% continue %}
{% else %}
{
"title":"{{ page.title }}",
"content":"{{ page.content | strip_html | strip_newlines }}",
"href":"{{ page.url }}"
}
{% endunless %}
{% unless forloop.last %},{% endunless %}
{% endfor %}
]
The end of the resulting JSON file looks like this:
{
"title":"Test Search",
"content":" ",
"href":"/search.html"
}
,
]
How do I get rid of the trailing comma? Thank you in advance.
I think your problem is that your last loop iteration is one that has no title.
Try to prepend the comma. That way you don't have to look in the future:
{% assign isFirst = true %}
{% for page in site.pages %}
{% unless page.title %}{% continue %}{% endunless %}
{% unless isFirst %},{% endunless %}
{% assign isFirst = false %}
{
"title": {{ page.title | jsonify }},
"content": {{ page.content | strip_html | strip_newlines | jsonify }},
"href": {{ page.url | jsonify }}
}
{% endfor %}
Edit: You should also use the jsonify filter to ensure proper escaping of quotes and other characters.
Try using {% raw %} to escape your non-liquid brackets, this can cause Jekyll to misbehave without throwing any errors.
If that still leads to odd behavior try creating a seperate array like the following example, which is very similar to the case from OP.
{% assign suggestions = "" | split: ',' %}
{%- for item in site[page.collection] -%}
{%- unless item.url == page.url -%}
{% assign suggestions = suggestions | push: item %}
{%- endunless -%}
{%- endfor -%}
<script id="suggest-json" type="application/json">
[
{%- for item in suggestions -%}
{% assign images = item.media | where: "type", "image" %}
{% assign image = images.first %}
{% raw %}
{
{% endraw %}
"url": "{{ site.baseurl }}{{ item.url }}",
"title": {{ item.title | jsonify }},
"subject": {{ item.game | jsonify }},
"image": {{ image.thumbnail | jsonify }},
"alt": {{ image.thumbnail_alt | jsonify }}
{% raw %}
}
{% endraw %}
{%- unless forloop.last -%},{%- endunless -%}
{%- endfor -%}
]
</script>
The problem may be that Jekyll doesn't actually consider the forloop.index as last because it isn't technically. You can avoid this by creating a new array either with a loop or the 'where' tag (loop used above).
The forloop.last should now behave properly, hope this helps.