diff --git a/bootstrap/routes.go b/bootstrap/routes.go index a4af90f..ca46cb5 100644 --- a/bootstrap/routes.go +++ b/bootstrap/routes.go @@ -18,9 +18,11 @@ along with this program. If not, see .Rawley Fow */ import ( + "net/http" + "strings" + "github.com/gin-gonic/gin" "gitlab.com/rawleyifowler/site-rework/controllers" - "net/http" ) // Initializes all routes from the controller package @@ -34,12 +36,20 @@ func InitializeRoutes(router *gin.Engine) { } // Returns a handler function to serve a page based on the template that is inputted -func ServePage(temp string) gin.HandlerFunc { +func ServePage(temp string) gin.HandlerFunc { + title := strings.Split(temp, ".")[0] + if title == "index" { + // Index is the home page so we don't care + title = "" + } else { + // Else title it accordingly + title = " | " + title + } return func(c *gin.Context) { c.HTML( http.StatusOK, temp, - gin.H{}, - ) + struct{ Title string }{Title: title}, + ) } } diff --git a/controllers/blog.go b/controllers/blog.go index 280686c..dcf9bcc 100644 --- a/controllers/blog.go +++ b/controllers/blog.go @@ -64,17 +64,21 @@ func RegisterBlogGroup(r *gin.RouterGroup) { func RenderBlogPage(c *gin.Context) { posts := GetAllBlogPosts() if posts == nil { - c.HTML(http.StatusInternalServerError, "internal_server_error.tmpl", gin.H{}) + c.HTML(http.StatusInternalServerError, "internal_server_error.tmpl", models.Page{Title: " | 500"}) } else { - c.HTML(http.StatusOK, "blog.tmpl", posts) + c.HTML(http.StatusOK, "blog.tmpl", struct { + Posts []models.BlogPost + Title string + }{Posts: *posts, Title: " | blog"}) } } func RenderIndividualBlogPost(c *gin.Context) { bp := GetBlogPostById(c.Param("url")) if bp == nil { - c.HTML(http.StatusNotFound, "not_found.tmpl", gin.H{}) + c.HTML(http.StatusNotFound, "not_found.tmpl", models.Page{Title: " | 404"}) } else { + // Blog posts handle the title themselves c.HTML(http.StatusOK, "blog_post.tmpl", struct { Post *models.BlogPost Captcha [2]int diff --git a/models/page.go b/models/page.go new file mode 100644 index 0000000..523a556 --- /dev/null +++ b/models/page.go @@ -0,0 +1,20 @@ +package models + +/* Copyright (C) 2022 Rawley Fowler +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see .Rawley Fowler, 2022 +*/ + +type Page struct { + Title string +} diff --git a/templates/blog.tmpl b/templates/blog.tmpl index 961d1fd..5e0175c 100644 --- a/templates/blog.tmpl +++ b/templates/blog.tmpl @@ -1,6 +1,6 @@ {{ template "header.tmpl" . }}

Blog posts

-{{ range $val := . }} +{{ range $val := .Posts }}
{{ $val.Title }} diff --git a/templates/blog_post.tmpl b/templates/blog_post.tmpl index ea419b8..c8c7b5b 100644 --- a/templates/blog_post.tmpl +++ b/templates/blog_post.tmpl @@ -1,4 +1,14 @@ -{{ template "header.tmpl" . }} + + + + + + + + {{ .Post.Title }} + + +{{ template "nav.tmpl" . }}

{{ .Post.Title }}

{{ .Post.Content | UnescapeHTML }}
diff --git a/templates/header.tmpl b/templates/header.tmpl index f16cebd..943d930 100644 --- a/templates/header.tmpl +++ b/templates/header.tmpl @@ -6,7 +6,7 @@ - rawley.xyz + rawley.xyz{{ .Title }} {{ template "nav.tmpl" . }}