I am not sure what your .Data or $data is, but in general you do it like this (very abstract, sorry):
define yearVar as zero
define monthVar as zero
range data
if discovery date > format as year only is different than yearVar then
set yearVar to year
print year
if discovery date > format as month only is different than monthVar then
set monthVar to month
print month
print current item
end
very abstract, sorry, but that’s the way to go I think.
My data is a json file, {{ $data := $.Site.Data.articles }}.
I think I understand your abstract example, set variables for year and month and keep checking at which point in time we’re at. Sounds like something I can achieve.
Edit: May need to be sorted by discovery_date before I use it though.
I am pretty sure that you can leave the $dataYear and $dataMonth declaration out of the header part and define them where you create them with dateFormat because you are inside of a range. “Loops” like range and with have their own data-declaration and after range is ended they stop existing. But if it works, leave like it is. No need to change something that works, hehe.
yes you are right, variables in Hugo only exist in the scope of the range or if where they are first declared. I came into the habit of always declaring them outside of that scope in case I want to use them later for something different. It was mostly a result of constantly forgetting that I had only used them inside that one specific scope.