I am attempting to sort RegularPages by a json list of permalinks. This json file represents the most viewed posts, so I’d like to retain the order presented in the json file.
I’ve accomplished this, but the method I used seems inefficient. If possible I’d like to cut down my build times as much as possible since I am working with many 100s of posts.
As you will see in my code below, I a nested ranges to accomplish this. My original attempt ranged through RegularPages and just checked if the page RelPermalink was present in the json file - that obviously resulted in a list that was not ordered properly.
Even though I can create this list more effeciently, in a roundabout way, by ranging through the RegularPages, creating a map of RelPermalinks (key) and File.Paths (value) and then ranging through the json data and cross referencing against the map to see if the json permalink is a RegularPage. Then using .GetPage from the .File.Path - that results in a list that I can not paginate.
I am wondering if I am over thinking this and there is a more simple and efficient way to accomplish it.
Any help would be greatly appreciated.
{{ define "main" }}
<div class="container">
{{ partial "subheader" . }}
<!-- popular.json contains RelPermalinks for most of the item pages, sorted by views -->
<!-- I don't know how to sort RegularPages by an ordered list of RelPermalinks -->
<!-- this array will store the pages ordered by the popular.json file -->
{{ $popular := slice }}
<!-- range over the json data -->
{{ range $.Site.Data.popular.feed.entry }}
<!-- get the RelPermalink from json -->
{{ $permalink := index .title "$t" }}
<!-- range over the item pages -->
{{ range where $.Site.RegularPages "Type" "items" }}
<!-- if permalink from json and page permalink match, add page to $popular -->
<!-- this is being done to retain the proper order of item posts -->
{{ if eq $permalink .RelPermalink }}
{{ $popular = $popular | append . }}
{{ end }}
{{ end }}
{{ end }}
<!-- range over $popular and paginate -->
<div class="item-feed list">
{{ range (.Paginate $popular).Pages }}
{{ partial "item" (dict "context" . "type" "list") }}
{{ end }}
</div>
{{ partial "paginator.html" . }}
</div>
{{ end }}
Update
Going about this a slightly different way results in faster builds, but I am still curious if there is a better way than this second attempt, pasted below:
{{ define "main" }}
<div class="container">
{{ partial "subheader" . }}
{{ $popular := slice }}
{{ $items := dict }}
{{ range where .Site.RegularPages "Type" "items" }}
{{ $item := dict .RelPermalink .File.Path }}
{{ $items = merge $items $item }}
{{ end }}
{{ range $.Site.Data.popular.feed.entry }}
{{ $permalink := index .title "$t" }}
{{ if isset $items $permalink }}
{{ $path := index $items $permalink }}
{{ with $.Site.GetPage $path }}
{{ $popular = $popular | append . }}
{{ end }}
{{ end }}
{{ end }}
<div class="item-feed list">
{{ range (.Paginate $popular).Pages }}
{{ partial "item" (dict "context" . "type" "list") }}
{{ end }}
</div>
{{ partial "paginator.html" . }}
</div>
{{ end }}