How to assign empty page list to variable?

Hi. Working on a simple implementation of the pinned post (just on the landing page) in the ZZO template, I would like to be able to assign an empty list of Pages to a variable. However, I haven’t found any working syntax how to do it (I’m new to Hugo/Go templates).

Sample code:

{{ $filteredPages := .Site.RegularPages }}
{{ range $.Param "notAllowedTypesInHome" }}
    {{ $filteredPages = (where $filteredPages "Type" "!=" (lower .)) }}
{{ end }}

// ↓↓↓ - this line
{{ $filteredPagesPinned := (where $filteredPages "Params.pinned" "==" true) }}

{{ $filteredPages := $filteredPages | intersect (where $filteredPages "Params.pinned" "!=" true) }}
{{ $filteredPages := $filteredPages | union ($filteredPagesPinned) }}
{{ $paginator := .Paginate $filteredPages }}
{{ range $paginator.Pages }}
    {{ .Render "summary" }}
{{ end }}
{{ partial "pagination/pagination" . }}

I would like to replace the separated line with something like:

{{ $filteredPagesPinned := []Page{} }}
{{ if .Site.Params.enablePinnedPosts }}
    {{ $filteredPagesPinned = (where $filteredPages "Params.pinned" "==" true)
{{ end }}

Is it possible?

Btw, I know, I can generate an empty list with dummy filtering of .Site.RegularPages or use some other approach:

{{ if .Site.Params.enablePinnedPosts }}
   {{ $filteredPagesPinned := (where $filteredPages "Params.pinned" "==" true) }}
   {{ $filteredPages = $filteredPages | union ($filteredPagesPinned) }}
{{ end }}

(with one extra change to do not override $filteredPages in the previous line), however, my approach with an empty list seems somehow more natural for me and I would like to know if I can achieve it somehow in Hugo.

In a case it is not trivial, what is the cheapest way to generate an empty collection using an existing list of pages?

Currently I have:

{{ $filteredPagesPinned := (where .Site.RegularPages "Type" "==" "not-existing-type-of-page") }}
{{ if .Site.Params.enablePinnedPosts }}
    {{ $filteredPagesPinned = (where $filteredPages "Params.pinned" "==" true) }}
{{ end }}
{{ $filteredPages := $filteredPages | intersect (where $filteredPages "Params.pinned" "!=" true) }}
{{ $filteredPages := $filteredPages | union ($filteredPagesPinned) }}
{{ $paginator := .Paginate $filteredPages }}
...
{{ $filteredPagesPinned := slice }}

Will be … mostly what you want. It will not be of the correct type, but you can range over it etc.

The cheapest way of creating an empty Pages collection may be something like this:

{{ site.RegularPages | first 0 }}
1 Like
{{ $filteredPagesPinned := slice }}

It seems the type doesn’t matter in that case and slice works fine. Thanks!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.