How would you correct and if possible, simplify this expression (which is invalid) ?
{{ with where (uniq (slice (.Site.RegularPages.Related .) (apply .Params.relatedPages “.GetPage” “.”))) “Params.bookHidden” “!=” true | first 3 }}
I wanted to be able to specify pages I absolutely want displayed as suggestions, and fill the other spots with the automatic algorithm. But I do not know how to write in a single expression “mind .Params.relatedPages only if it’s actually set !”.
I tried with a if statement but this fails miserably:
{{ with .Params.relatedPages }}
{{ $related = uniq (where (slice (apply . ".Site.GetPage" ".Page") (.Site.RegularPages.Related .Page)) "Params.bookHidden" "!=" true ) | first 3 }}
{{else}}
{{ $related = where (.Site.RegularPages.Related .) "Params.bookHidden" "!=" true | first 3 }}
{{end}}
ERROR 2023/06/19 23:03:26 Rebuild failed: render: failed to render pages: render of “page” failed: “/home/drm/WEBSITE/layouts/_default/baseof.html:33:5”: execute of template failed at <partial “docs/inject/content-after” .>: error calling partial: “/home/drm/WEBSITE/layouts/partials/docs/inject/content-after.html:27:36”: execute of template failed at <apply . “.Site.GetPage” “.Page”>: error calling apply: can’t find function .Site.GetPage
{{/* Get explicity related pages, then sort by something. */}}
{{ $p1 := slice }}
{{ range .Params.relatedPages }}
{{ $p1 = $p1 | append (site.GetPage .) }}
{{ end }}
{{ $p1 = sort $p1 "Params.title" "asc" }}
{{/* Get implicitly related pages, then sort by something. */}}
{{ $p2 := site.RegularPages.Related . }}
{{ $p2 = sort $p2 "Params.title" "asc" }}
{{/* Union, filter, and limit. */}}
{{ $p := where (union $p1 $p2) "Params.bookHidden" "ne" true | first 3 }}
{{/* Render list. */}}
{{ with $p }}
<p>Related pages:
<ul>
{{ range . }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
</p>
{{ end }}
The final page collection is unique due to the union function.
Also note that the sorting steps are optional. For example, to keep the explicitly related pages in the order that you specified in front matter, remove the sort step from the first block of code above.