From e7e01fedce0c4a5b7e6e5eec41ed5e97a1bf1979 Mon Sep 17 00:00:00 2001 From: BigCoke233 Date: Sat, 6 Dec 2025 16:10:43 +0800 Subject: refactor: library section template - use Context to get pages, instead of filter the whole site's pages - use one tempalte `section.html` for both library home and year lists --- layouts/library/section.html | 45 +++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'layouts/library/section.html') diff --git a/layouts/library/section.html b/layouts/library/section.html index a7df419a..cab61e51 100644 --- a/layouts/library/section.html +++ b/layouts/library/section.html @@ -1,17 +1,34 @@ {{ define "main" }} + +{{ $isLibraryHome := eq .RelPermalink "/library/" }} +{{ $books := .RegularPages }} +{{ if $isLibraryHome }} + {{ range .Sections }} + {{ $books = $books | append .RegularPages }} + {{ end }} +{{ end }} + +{{ $years := slice }} +{{ range .Sections }} + {{ $years = $years | append .Title }} +{{ end }}
{{ partial "header.html" . }}
- {{ $booksThisYear := where (where .Site.RegularPages "Section" "library") "Date.Year" now.Year }} + {{ if $isLibraryHome }} {{ partial "components/section-title" (dict - "Left" now.Year - "Right" (printf "共 %d 篇书评" (len $booksThisYear)) + "Left" "最近读的书" + "Right" (printf "共 %d 篇书评" (len $books)) )}} + {{ end }}
- {{ $first18BooksThisYear := first 18 $booksThisYear }} - {{ range $i, $book := $first18BooksThisYear }} + {{ $booksToShow := $books }} + {{ if $isLibraryHome }} + {{ $booksToShow = first 18 $books }} + {{ end }} + {{ range $i, $book := $booksToShow }}
{{ partial "items/book" (dict @@ -21,14 +38,11 @@ {{ end }}
- {{ $years := slice }} - {{ range where .Site.RegularPages "Section" "library" }} - {{ $year := string .Date.Year }} - {{ if not (in $years $year) }} - {{ $years = $years | append $year }} - {{ end }} - {{ end }} + + {{ if $isLibraryHome }} + + - {{ $goodBooks := where (where .Site.RegularPages "Section" "library") ".Params.rating" "==" 5 }} + + {{ $goodBooks := where $books ".Params.rating" "==" 5 }} {{ partial "components/section-title" (dict "Left" "推荐好书" "Right" (printf "共 %d 本" (len $goodBooks)) @@ -57,6 +72,10 @@
{{ end }}
+ + {{ end }} + +
{{ partial "footer.html" . }} -- cgit v1.2.3