hug
May 9, 2022, 12:26am
1
I’m using
$.Scratch.SetInMap "pages" (string (time $page.Params.date).Unix) $page
to populate a dictionary with pages. Afterwards, I want to iterate over that dict.
$range := $.Scratch.GetSortedMapValues "pages"
works, but I need to invert the sort order. How can I do this?
As GetSortedMapValues
does not seem to accept additional arguments, I tried multiple solutions:
sort ($.Scratch.Get "pages"))
sort ($.Scratch.GetSortedMapValues "pages")
Both give:
error calling sort: sequence must be provided
Any ideas on how to iterate over a scratch map in a specified order? Thanks a lot in advance!
hug
May 9, 2022, 9:17pm
3
Thanks for replying! I tried this, unfortunately it doesn’t work.
sort ($.Scratch.GetSortedMapValues "pages") "Date" "desc"
sort ($.Scratch.GetSortedMapValues "pages") "key" "desc"
sort ($.Scratch.GetSortedMapValues "pages") "value" "desc"
All give the same error:
executing "main" at <sort ($.Scratch.GetSortedMapValues "pages") "Date" "desc">: error calling sort: sequence must be provided
When running just {{ ($.Scratch.GetSortedMapValues "pages") }}
, I get:
[Page(/termine/test1.md) Page(/termine/test2.md)]
This does look like a sequence to me. Or am I missing something?
I also tried running {{ ($.Scratch.Get "pages") }}
which gives:
map[1648544400:Page(/termine/test1.md) 1648978200:Page(/termine/test2.md)]
But calling sort on that:
sort ($.Scratch.Get "pages")
sort ($.Scratch.Get "pages") "key"
sort ($.Scratch.Get "pages") "value"
sort ($.Scratch.Get "pages") "desc"
sort ($.Scratch.Get "pages") "value" "desc"
sort ($.Scratch.Get "pages") "date" "desc"
also always results in that same error.
Thanks a lot for any ideas!
{{ $pages := .Scratch.GetSortedMapValues "pages" }}
{{ range seq (sub (len $pages) 1) 0 }}
{{ with index $pages . }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a><br>
{{ end }}
{{ end }}
1 Like
hug
May 10, 2022, 12:04am
5
Hi, thanks for your reply! It still failed at len $pages
:
executing "main" at <len $mypages>: error calling len: reflect: call of reflect.Value.Type on zero Value
However, that gave me the hint that probably the layout gets called by hugo for an empty list, and by surrounding the code with
{{ with .Pages }}
[...]
{{ end }}
the error message disappears. If only hugo could print debug output to the console, I would have noticed this earlier.
Now I can even sort the scratch map as simple as
sort ($.Scratch.Get "pages")
however, I still can’t get it to sort descending by key. The manual only describes how to sort ascending by key or descending by value, any ideas?
Thanks a lot!
The example I provided works. Try it:
git clone --single-branch -b hugo-forum-topic-38523 https://github.com/jmooring/hugo-testing hugo-forum-topic-38523
cd hugo-forum-topic-38523
hugo server
And look at layouts/_default/home.html.
If you need additional assistance, please provide a link to the public repository for your project.
1 Like
hug
May 10, 2022, 1:03am
7
Yes, you’re right. I didn’t see that the seq
inverts the sort order by iterating from n
to 0
. Thanks a lot for your work!
system
Closed
May 12, 2022, 1:03am
8
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.