FoxDeploy
FoxDeploy

Reputation: 13557

Jekyll Liquid Syntax, how to dereference a property

I am grouping the posts on my site using this liquid tag.

{% assign postsForYear = site.posts | group_by_exp:"post", "post.date | date: '%Y'" | where: "name", "2020" %} 

When I echo the postForYear out to screen, I see this:

{"name"=>"2020", "items"=>[#, #, #, #, #, #], "size"=>6} 0

Which makes sense, as I have six posts for that year. However, I am trying to dereference that object and get the .size property, which I can see in the output...I can't figure out the syntax!

How to get the .Size property?

None of these work.

{{ postsForYear.size }}
{{ postsForYear.items | size }}

After that, I would love to learn how to foreach my way through the posts...this also seems simple but doesn't work!

Upvotes: 1

Views: 128

Answers (1)

FoxDeploy
FoxDeploy

Reputation: 13557

Ah, I figured it out. Strangely, the postsForYear array was actually treated like an array itself, so I had to index into the first position to get to the properties.

{% assign postsForYear = site.posts | 
    group_by_exp:"post", "post.date | date: '%Y'" | where: "name", "2020" %} 

###doesn't work
{{ postsForYear.items | size }}

### does work
{{ postsForYear[0].items | size }}

### example
Posts for year {{ postsForYear[0].name }}, total posts {{ postsForYear[0].size }} 

>Posts for year 2020, total posts 6  

And to answer my other question, how to foreach your way through:

{% for post in postsForYear[0].items %}
   <h1>{{post.title}}</h1><br>
{% endfor %}

enter image description here

Upvotes: 2

Related Questions