Skip to content

Commit 0ed2180

Browse files
feat: Add filtering for blog posts by tag and author
This commit introduces filtering functionality to the blog page, allowing users to filter posts by tags and authors. It also includes a 'Featured Posts' section. - Adds dropdown menus for tag and author selection. - Uses URL query parameters to apply filters. - Defines 'tags' and 'authors' as taxonomies in the site configuration.
1 parent 1db5170 commit 0ed2180

File tree

5 files changed

+100
-2
lines changed

5 files changed

+100
-2
lines changed

config.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ enableRobotsTXT = false
1616
filename = "sitemap.xml"
1717
priority = 0.5
1818

19+
[taxonomies]
20+
tag = "tags"
21+
author = "authors"
22+
1923
[params]
2024
# In most cases you will only want to set the google_analytics_id OR the google_tag_manager_id.
2125
# If you have Google Analytics included in your GTM tags don't put your GA ID here.

content/blog/aws-ecs-fargate-vs-self-managed-ec2.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
---
22
title: "AWS ECS: FARGATE vs self-managed EC2"
33
authorIds: ["saumitra"]
4+
authors: ["saumitra"]
45
date: 2024-08-13
56
draft: false
67
featured: true
78
weight: 1
9+
tags: ["aws", "ecs", "fargate", "ec2"]
810
---
911

1012
Cooking Up Cloud: Fargate or EC2—Which Kitchen Suits You?

content/blog/building-internal-tools.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
---
22
title: "Building internal tooling in legacy systems"
33
authorIds: ["abhishek"]
4+
authors: ["abhishek"]
45
date: 2024-04-11
56
draft: false
67
featured: true
78
weight: 1
89
sitemap:
910
changefreq: 'monthly'
1011
priority: 1
12+
tags: ["engineering", "legacy-systems"]
1113
---
1214

1315

hugo.log

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
port 1313 already in use, attempting to use an available port
2+
Watching for changes in /app/{assets,content,data,layouts,static}
3+
Watching for config changes in /app/config.toml
4+
Start building sites …
5+
hugo v0.145.0-666444f0a52132f9fec9f71cf25b441cc6a4f355+extended linux/amd64 BuildDate=2025-02-26T15:41:25Z VendorInfo=gohugoio

layouts/blog/list.html

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{{ define "body_classes" }}page-services-list{{ end }}
22

33
{{ define "main" }}
4+
{{ .Scratch.Set "tag" (.Param "tag") }}
5+
{{ .Scratch.Set "author" (.Param "author") }}
46
<div class="container">
57
<div class="row">
68
<div class="col-12 text-center mt-8">
@@ -10,8 +12,59 @@
1012
</div>
1113

1214
<div class="container pt-6 pb-6">
15+
<div class="row mb-4">
16+
<div class="col-12">
17+
<div class="d-flex justify-content-center">
18+
<div class="mx-2">
19+
<select id="tag-filter" class="form-select" onchange="filterPosts()">
20+
<option value="">All Tags</option>
21+
{{ range .Site.Taxonomies.tags.ByCount }}
22+
<option value="{{ .Name | urlize }}" {{ if (eq ($.Scratch.Get "tag") (.Name | urlize)) }}selected{{ end }}>{{ .Name }} ({{ .Count }})</option>
23+
{{ end }}
24+
</select>
25+
</div>
26+
<div class="mx-2">
27+
<select id="author-filter" class="form-select" onchange="filterPosts()">
28+
<option value="">All Authors</option>
29+
{{ range .Site.Taxonomies.authors.ByCount }}
30+
<option value="{{ .Name | urlize }}" {{ if (eq ($.Scratch.Get "author") (.Name | urlize)) }}selected{{ end }}>{{ .Page.Title }} ({{ .Count }})</option>
31+
{{ end }}
32+
</select>
33+
</div>
34+
</div>
35+
</div>
36+
</div>
37+
38+
{{ $pages := .Pages }}
39+
{{ $featured := where $pages "Params.featured" "eq" true }}
40+
{{ $regular := where $pages "Params.featured" "ne" true }}
41+
42+
{{ $tag := .Scratch.Get "tag" }}
43+
{{ $author := .Scratch.Get "author" }}
44+
45+
{{ if $tag }}
46+
{{ $regular = where $regular "Params.tags" "intersect" (slice $tag) }}
47+
{{ end }}
48+
{{ if $author }}
49+
{{ $regular = where $regular "Params.authors" "intersect" (slice $author) }}
50+
{{ end }}
51+
52+
{{ if $featured }}
53+
<div class="featured-posts">
54+
<h2 class="text-center mb-4">Featured Posts</h2>
55+
<div class="row">
56+
{{ range $featured }}
57+
<div class="col-12">
58+
<div>{{ .Render "summary" }}</div>
59+
</div>
60+
{{ end }}
61+
</div>
62+
</div>
63+
<hr class="my-5" />
64+
{{ end }}
65+
1366
<div class="row">
14-
{{ $paginator := .Paginate .Pages }}
67+
{{ $paginator := .Paginate $regular }}
1568
{{ range $paginator.Pages }}
1669
<div class="col-12">
1770
<div>{{ .Render "summary" }}</div>
@@ -22,4 +75,36 @@
2275
{{ partial "pagination.html" . }}
2376
</div>
2477
</div>
25-
{{ end }}
78+
79+
<script>
80+
function filterPosts() {
81+
const tag = document.getElementById('tag-filter').value;
82+
const author = document.getElementById('author-filter').value;
83+
let url = '/blog/';
84+
const params = new URLSearchParams();
85+
if (tag) {
86+
params.append('tag', tag);
87+
}
88+
if (author) {
89+
params.append('author', author);
90+
}
91+
const queryString = params.toString();
92+
if (queryString) {
93+
url += `?${queryString}`;
94+
}
95+
window.location.href = url;
96+
}
97+
98+
document.addEventListener('DOMContentLoaded', () => {
99+
const params = new URLSearchParams(window.location.search);
100+
const tag = params.get('tag');
101+
const author = params.get('author');
102+
if (tag) {
103+
document.getElementById('tag-filter').value = tag;
104+
}
105+
if (author) {
106+
document.getElementById('author-filter').value = author;
107+
}
108+
});
109+
</script>
110+
{{ end }}

0 commit comments

Comments
 (0)