Range modified pages and count them properly [SOLVED]

I have a potentially embarrassing issue.

I use lastmod in front matter for the purpose of listing recently changed content.

I have the following code:

<div class="d-none d-lg-block">
<div class="card-deck">
{{ range first 7 (where .Site.Pages.ByLastmod.Reverse "Section" "star")  }}
{{ if not (eq .Title "Star") }}
{{ if not (eq .Date .Lastmod) }}
<div class="card card-recent card-img-top mb-2">
{{ partial "recent-content-image-card" . }}
<div class="card-body">
{{ partial "recent-content-text-card" . }}
</div><!-- /card --> 
{{ end }}
{{ end }}
{{ end }}
</div><!-- /deck -->

It works and all. The problem is, when i add new content, i have to manually correct (increase by 1) range first X or it only shows last two. I want it to show last 3.

The problem is probably in “skipping the content” and me not knowing how the counting is done. More specifically, i think the problem is where it checks whether or not the lastmod is same as the date. This is done to prevent listing of new content or unchanged content.

The code i made seems to be inadequate to handle that.

Anyone with an idea on how to improve the code so there will be no need for manual corrections?

Try doing your filtering beforehand:

{{ $pages := where .Site.Pages.ByLastmod.Reverse "Section" "star" }}
{{ $pages = where $pages ".Title" "ne" "Star" }}
{{ $pages = where $pages ".Date" "ne" ".Lastmod" }}
{{ range first 7 $pages }}

Hmm ok.

I do {{ $pages := where .Site.Pages.ByLastmod.Reverse "Section" "star" }} and get all results as expected.
Then i do {{ $pages := where $pages ".Title" "ne" "Star" }} and get 1 less result, all ok.
Then i do {{ $pages := where $pages ".Date" "ne" "Lastmod" }} and get 0 results, not expected. :thinking:

Skipping the date check i do the {{ range first 3 $pages }} and it works, even if i add or remove content it shows 3. That is great. However it also shows content i added last, that has not been modified.

Not all my content files (included the most recent) have lastmod set to a specific date. It is empty, it is in the front matter but no date is set as it would seem it defaults to the same number as set in date.I am beginning to think that might be the problem. Then again, having content that has not been updated listed under updated is not what i am trying to do.

One solution might be to set lastmod to 0 in all not modified content. 0 defaults to January 1, 1970 and then in template where lastmod is displayed an if check to display it only if the date is not January 1, 1970 and for the last modified to use it as {{ $pages := where $pages "0" "ne" "Lastmod" }} but i am not that enthusiastic about this “solution”.

Since your .Date and .Lastmod formats do not match, you’ll need to make that happen. For examples see: Paginating future content (events)

Give this a shot:

{{ $pages := slice }}

{{ range site.RegularPages }}
  {{ if gt .Lastmod .Date }}
    {{ $pages = $pages | append . }}
  {{ end }}
{{ end }}

{{ $pages = where $pages "Section" "eq" "star" }}
{{ $pages = where $pages ".Title" "ne" "Star" }}

{{ range first 3 $pages }}

Not exactly working out or i just don’t know how to do it this way. My programming abilities are limited.

Since my last reply i added haschanged param to front matter for content that has not been modified. This param is used in if statement to determine whether or not to display/print to screen lastmod value. I do not have a lot of such content at this point so manually going through each was doable.

Then i also changed the lastmod for content that has not been modified to 0.


{{ range first 3 (where .Site.Pages.ByLastmod.Reverse "Section" "star")  }}
{{ if not (eq .Title "Star") }}
{{ if not (eq "0" .Lastmod) }}

now works as expected. I think. I did some basic testing. Based on archetype, when new content is added, it already has lastmod at 0 and haschanged at “0” so it no longer interferes with the display of recently changed content and is properly displayed under changed content if haschanged is removed and lastmod is given a date.

I realize this is probably not an elegant solution. Just saw your most recent reply. Haven’t tried that but will make a note of it. Thx for taking the time. Will mark the topic solved for now.