added dynamic titles to pages for SEO

This commit is contained in:
rawleyIfowler
2022-02-27 12:24:52 -06:00
parent a2779c487c
commit 161f68ba85
6 changed files with 54 additions and 10 deletions

View File

@@ -18,9 +18,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.Rawley Fow
*/ */
import ( import (
"net/http"
"strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gitlab.com/rawleyifowler/site-rework/controllers" "gitlab.com/rawleyifowler/site-rework/controllers"
"net/http"
) )
// Initializes all routes from the controller package // 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 // 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) { return func(c *gin.Context) {
c.HTML( c.HTML(
http.StatusOK, http.StatusOK,
temp, temp,
gin.H{}, struct{ Title string }{Title: title},
) )
} }
} }

View File

@@ -64,17 +64,21 @@ func RegisterBlogGroup(r *gin.RouterGroup) {
func RenderBlogPage(c *gin.Context) { func RenderBlogPage(c *gin.Context) {
posts := GetAllBlogPosts() posts := GetAllBlogPosts()
if posts == nil { 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 { } 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) { func RenderIndividualBlogPost(c *gin.Context) {
bp := GetBlogPostById(c.Param("url")) bp := GetBlogPostById(c.Param("url"))
if bp == nil { if bp == nil {
c.HTML(http.StatusNotFound, "not_found.tmpl", gin.H{}) c.HTML(http.StatusNotFound, "not_found.tmpl", models.Page{Title: " | 404"})
} else { } else {
// Blog posts handle the title themselves
c.HTML(http.StatusOK, "blog_post.tmpl", struct { c.HTML(http.StatusOK, "blog_post.tmpl", struct {
Post *models.BlogPost Post *models.BlogPost
Captcha [2]int Captcha [2]int

20
models/page.go Normal file
View File

@@ -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 <https://www.gnu.org/licenses/>.Rawley Fowler, 2022
*/
type Page struct {
Title string
}

View File

@@ -1,6 +1,6 @@
{{ template "header.tmpl" . }} {{ template "header.tmpl" . }}
<h2>Blog posts</h2> <h2>Blog posts</h2>
{{ range $val := . }} {{ range $val := .Posts }}
<div class="post-record"> <div class="post-record">
<div> <div>
<a class="post-record-title" href="/blog/post/{{ $val.Url }}">{{ $val.Title }}</a> <a class="post-record-title" href="/blog/post/{{ $val.Url }}">{{ $val.Title }}</a>

View File

@@ -1,4 +1,14 @@
{{ template "header.tmpl" . }} <html>
<head>
<meta lang="en">
<meta charset="UTF-8">
<meta name="keywords" content="blog resume rawley-fowler rawleyportfolio rawleyxyz rawleyfowler RF rawley fowler Rawley Fowler portfolio RawleyFowler RawleyXYZ rawleyXYZ RawleyXYZ">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/index.css" rel="stylesheet" type="text/css">
<title>{{ .Post.Title }}</title>
</head>
<body>
{{ template "nav.tmpl" . }}
<div> <div>
<h2>{{ .Post.Title }}</h2> <h2>{{ .Post.Title }}</h2>
<div>{{ .Post.Content | UnescapeHTML }}</div> <div>{{ .Post.Content | UnescapeHTML }}</div>

View File

@@ -6,7 +6,7 @@
<meta name="keywords" content="blog resume rawley-fowler rawleyportfolio rawleyxyz rawleyfowler RF rawley fowler Rawley Fowler portfolio RawleyFowler RawleyXYZ rawleyXYZ RawleyXYZ"> <meta name="keywords" content="blog resume rawley-fowler rawleyportfolio rawleyxyz rawleyfowler RF rawley fowler Rawley Fowler portfolio RawleyFowler RawleyXYZ rawleyXYZ RawleyXYZ">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/index.css" rel="stylesheet" type="text/css"> <link href="/static/index.css" rel="stylesheet" type="text/css">
<title>rawley.xyz</title> <title>rawley.xyz{{ .Title }}</title>
</head> </head>
<body> <body>
{{ template "nav.tmpl" . }} {{ template "nav.tmpl" . }}