I'm trying to group elements from a custom jekyll collection by the value of one of their metadata (date).
So, I'm doing {% for elt in site.my_collection | group_by: "date" %} but it loops through the collection normally, just like I've written {% for elt in site.my_collection %}.
Even stranger is if I write in my template {{ site.my_collection | group_by: "date" }}, then it displays correctly the grouped collection [{"name" => "day1", "items" => [#, #, #]}, {"name" => "day2", "items" => [#]}].
What am I doing wrong? Is this because I'm using a custom collection?
Thanks.
You cannot loop and sort/group at the same time.
You have to assign and sort/group then loop
That's true for Jekyll elements like pages, posts or collection.
{% assign collection = site.my_collection | group_by: "date" %}
{% for group in collection %}
<h3>{{ group.name | date: "%-d %B %Y" }}</h3>
<ul>
{% for item in group.items %}
<li>{{item.data}}</li>
{%endfor%}
</ul>
{%endfor%}
Related
I have a list of products, each with a list of features of nested yaml lists
- product: Product n1
features:
- feature1
- feature2
- feature3
- product: Product n2
features:
- feature1
- feature2
- product: Product n3
features:
- feature1
- feature2
- feature3
- feature4
- feature5
Goal: create a list of divs, one for each product, with a title and the list of features.
I tried using for subitem in item but I can't get it to work - any ideas?
I solved the problem with the following code:
{% for product in site.data.products %}
<div>
<h2>{{ product.product }}</h2>
<ul>
{% for feature in product.features %}
<li>{{ feature }}</li>
{% endfor %}
</ul>
</div>
{% endfor %}
I am new to Liquid, so forgive me if this is obvious.
I have some data whose titles are prefixed with A1, B10, C64, etc. before the real text I want to sort on, for example, "A1 Something" "B10 Nothing" "A23 Another".
If I use
{% assign itemssorted = product.ds_related_products | sort: 'title' %}
{% for item in itemssorted %}
then I get the items sorted according to the prefixes, whereas what I want is the items sorted by the text after the prefixes.
Is there something like
{% assign itemssorted = product.ds_related_products | sort: 'title' | split: ' ' .... %}
I can use to implement this?
Try to change the sort:price for the variable you want to sort:
{% assign products = collection.products | sort: 'price' %}
{% for product in products %}
<h4>{{ product.title }}</h4>
{% endfor %}
<!-- products = "a", "b", "A", "B" -->
{% assign products = collection.products | sort: 'title' %}
{% for product in products %}
{{ product.title }}
{% endfor %}
I have data like this sorted by Category:
Category 1, Product 1, Amount
Category 1, Product 2, Amount
Category 2, Product 3, Amount
I like to print it using blade as
Category 1
Product 1 Amount
Product 2 Amount
....
Total Amount of Category 1 : xxxxx
Category 2
Product 3 Amount
....
Total Amount of Category 2 : xxxxx
I am assuming that $categories is the sorted multidimensional array with indexes category,product and amount.
#for ($i = 0; $i < count($categories) ; $i++)
<?php $category_product_toatl=0; ?>
{{ $categories[$i]['category'] }}
{{ $categories[$i]['product'] }} {{ $categories[$i]['amount']}}
<?php
$category_product_total= $category_product_total+ $categories[$i]['amount'];
?>
#for ($j = $i+1; $j < count($categories) ; $j++)
#if($categories[$i]['category']== $categories[$j]['category'])
{{ $categories[$j]['product'] }} {{ $categories[$j]['amount']}}
<?php
$category_product_total= $category_product_total+ $categories[$j]['amount'];
?>
#else
break;
#endif
#endfor
Total Amount of {{ $category[$i] }} : {{ $category_product_total }}
#endfor
I am trying to sort a collection of products by more than one filter. I want to sort my products by their tag ( collection.products | sort: 'tags' ) and by title ascending ( collection.products | sort: 'title' ). I have tried multiple variations of the following:
{% assign sortedProducts = collection.products | sort: 'title' | sort: 'tags' %}
{% assign sortedProducts = collection.products | sort: 'tags' | sort: 'title' %}
{% assign sortedProducts = collection.products | sort: 'title' %}
{% assign sortedProducts = sortedProducts | sort: 'tags' %}
{% assign sortedProducts = collection.products | sort: 'tags' %}
{% assign sortedProducts = sortedProducts | sort: 'title' %}
None of them work. I have seen questions with answers that involve filtering by tag and then sorting alphabetically, but I was hoping to sort by tag and title on the same page. If it's not possible, then I'll look more into filtering by tag before sorting.
Thanks!
Apparently with liquid tags you shouldn't leave spaces, like below(untested):
{% assign sortedProducts = collection.products|sort:'title'|sort:'tags' %}
{% assign sortedProducts = collection.products|sort:'tags'|sort:'title' %}
{% assign sortedProducts = collection.products|sort:'title' %}
{% assign sortedProducts = sortedProducts|sort:'tags' %}
{% assign sortedProducts = collection.products|sort:'tags' %}
{% assign sortedProducts = sortedProducts|sort:'title' %}
Then there's one more complication: pagination.
There is a pagination setting somewhere in your settings or theme settings. The default is 50 products per 'page'. The problem in my experience is when you ask it to sort it may only sorting within the first 50 products :(
I'm trying to display the created_at column in one of my views and I'm not having any luck. When I try using the below:
{{ date('d M Y - H:i:s', $object->created_at) }}
I get the following error: date() expects parameter 2 to be long, object given.
So I use dd() on $object->created_at and it gives me this:
object(Carbon\Carbon)#555 (3) { ["date"]=> string(26) "2015-01-22 14:36:37.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(19) "Africa/Johannesburg" }
So naturally I tried accessing the date with a foreach loop which didn't work and I also tried $object->created_at->date which gave me an error "Unknown getter 'date'"
How do I access the date from created_at?
Laravel uses Carbon. Then try this:
{{ date('d M Y - H:i:s', $object->created_at->timestamp) }}
Or this is the same without date function
{{ $object->created_at->format('d M Y - H:i:s') }}
Or use any method documented in the Carbon API
This is another method -
{{ date('F d, Y', strtotime($list->created_at)) }}