refactor: use .Params.mainSections to filter posts

It's recommended by Hugo's official doc: https://gohugo.io/functions/where/#mainsections
This commit is contained in:
Jimmy Cai 2020-09-06 13:15:38 +02:00
parent 47fb5af201
commit e176e29811
No known key found for this signature in database
GPG key ID: 3EA408E527F37B18
5 changed files with 16 additions and 17 deletions

View file

@ -9,7 +9,7 @@ title = "Example Site"
page = "/:slug/"
[params]
postSection = "post"
mainSections = ["post"]
favicon = ""
[params.sidebar]
emoji = "🍥"

View file

@ -4,11 +4,10 @@
{{ partialCached "sidebar/left.html" . }}
<main class="main full-width">
{{ $postSection := $.Site.Params.postSection }}
{{ $v1 := where .Site.RegularPages "Section" $postSection }}
{{ $v2 := where .Site.RegularPages "Params.hidden" "!=" true }}
{{ $.Scratch.Set "filtered" ($v1 | intersect $v2) }}
{{ $pag := .Paginate ($.Scratch.Get "filtered") }}
{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
{{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }}
{{ $filtered := ($pages | intersect $notHidden) }}
{{ $pag := .Paginate ($filtered) }}
<section class="article-list">
{{ range $index, $element := $pag.Pages }}

View file

@ -16,10 +16,10 @@
</div>
</div>
{{ $postSection := $.Site.Params.postSection }}
{{ $v1 := where .Site.RegularPages "Section" $postSection }}
{{ $v2 := where .Site.RegularPages "Params.hidden" "!=" true }}
{{ $filtered := $v1 | intersect $v2 }}
{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
{{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }}
{{ $filtered := ($pages | intersect $notHidden) }}
{{ range $filtered.GroupByDate "2006" }}
{{ $id := lower (replace .Key " " "-") }}
<div class="archive-group" id="{{ $id }}">

View file

@ -3,10 +3,10 @@
{{- $authorName := .Site.Author.name -}}
{{- if .IsHome -}}
{{- $v1 := where .Site.RegularPages "Type" "post" -}}
{{- $v2 := where .Site.RegularPages "Params.hidden" "!=" true -}}
{{- $filtered := $v1 | intersect $v2 -}}
{{- $pag := .Paginate ($filtered) -}}
{{ $pages := where .Site.RegularPages "Section" "in" .Site.Params.mainSections }}
{{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }}
{{ $filtered := ($pages | intersect $notHidden) }}
{{ $pag := .Paginate ($filtered) }}
{{ if .Paginator.HasPrev }}{{ .Paginator }} - {{ end }}{{ $siteTitle }}
{{- else if eq .Kind "term" -}}
{{- $pag := .Paginate (where .Data.Pages "Type" "post") -}}

View file

@ -4,9 +4,9 @@
</div>
<h1 class="widget-title">Archive</h1>
{{ $v1 := where .Site.RegularPages "Section" "post" }}
{{ $v2 := where .Site.RegularPages "Params.hidden" "!=" true }}
{{ $filtered := $v1 | intersect $v2 }}
{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
{{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }}
{{ $filtered := ($pages | intersect $notHidden) }}
{{ $archive := $filtered.GroupByDate "2006" }}
{{ range first .Site.Params.widgets.archive.limit ($archive) }}