summaryrefslogtreecommitdiff
path: root/layouts/library
diff options
context:
space:
mode:
authorBigCoke233 <hi@guhub.cn>2025-12-06 16:10:43 +0800
committerBigCoke233 <hi@guhub.cn>2025-12-06 16:10:43 +0800
commite7e01fedce0c4a5b7e6e5eec41ed5e97a1bf1979 (patch)
tree4b947238e4ce4c6c285acec4e0840bdab193602a /layouts/library
parent59eb9da5f80cccb3578d30bd0565283e9643fd9f (diff)
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
Diffstat (limited to 'layouts/library')
-rw-r--r--layouts/library/section.html45
-rw-r--r--layouts/library/yearlist.html18
2 files changed, 32 insertions, 31 deletions
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 }}
<article class="pagefind-exclude">
{{ partial "header.html" . }}
<div>
- {{ $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 }}
<div class="grid grid-cols-4 md:grid-cols-6 items-center gap-4 my-4">
- {{ $first18BooksThisYear := first 18 $booksThisYear }}
- {{ range $i, $book := $first18BooksThisYear }}
+ {{ $booksToShow := $books }}
+ {{ if $isLibraryHome }}
+ {{ $booksToShow = first 18 $books }}
+ {{ end }}
+ {{ range $i, $book := $booksToShow }}
<div class="{{ if gt $i 15 }}hidden md:block{{ end }}"
data-tooltip="{{ $book.Title }}">
{{ partial "items/book" (dict
@@ -21,14 +38,11 @@
{{ end }}
</div>
- {{ $years := slice }}
- {{ range where .Site.RegularPages "Section" "library" }}
- {{ $year := string .Date.Year }}
- {{ if not (in $years $year) }}
- {{ $years = $years | append $year }}
- {{ end }}
- {{ end }}
+ <!-- ONLY SHOW IN HOME SECTION / START -->
+ {{ if $isLibraryHome }}
+
+ <!-- === year list === -->
<div class="flex items-center my-8">
<a href="/library/2025" class="small-button">
{{ T "library.viewAllThisYear" }}
@@ -42,7 +56,8 @@
</p>
</div>
- {{ $goodBooks := where (where .Site.RegularPages "Section" "library") ".Params.rating" "==" 5 }}
+ <!-- === good books section === -->
+ {{ $goodBooks := where $books ".Params.rating" "==" 5 }}
{{ partial "components/section-title" (dict
"Left" "推荐好书"
"Right" (printf "共 %d 本" (len $goodBooks))
@@ -57,6 +72,10 @@
</div>
{{ end }}
</div>
+
+ {{ end }}
+ <!-- ONLY SHOW IN HOME SECTION / END -->
+
</div>
</article>
{{ partial "footer.html" . }}
diff --git a/layouts/library/yearlist.html b/layouts/library/yearlist.html
deleted file mode 100644
index 1405cb27..00000000
--- a/layouts/library/yearlist.html
+++ /dev/null
@@ -1,18 +0,0 @@
-{{ define "main" }}
- {{ partial "header.html" . }}
- <article class="pagefind-exclude">
- <div>
- <h2>{{ .Title }}</h2> <!-- 显示年份 -->
- <div class="grid grid-cols-4 md:grid-cols-6 items-center gap-4 my-4">
- {{ $year := .Title }}
- {{ range where .Site.RegularPages "Section" "library" }}
- {{ if eq (printf "%d" .Date.Year) $year }}
- <div data-tooltip="{{ .Title }}">
- {{ partial "items/book" (dict "ctx" .) }}</div>
- {{ end }}
- {{ end }}
- </div>
- </div>
- </article>
- {{ partial "footer.html" . }}
-{{ end }}