template moved into utils

This commit is contained in:
Chris kerr 2024-05-29 20:55:49 -04:00
parent 405b437c44
commit dbe81ba033
4 changed files with 12 additions and 12 deletions

View File

@ -1,14 +1,15 @@
package routes
import (
"GoStart/src/utils"
"net/http"
)
func ExampleHandler(w http.ResponseWriter, r *http.Request) {
data := PageData{
data := utils.PageData{
Title: "Example Page",
}
// Call renderTemplate with the specific template for this route
renderTemplate(w, data, "example.html")
// Call RenderTemplate with the specific template for this route
utils.RenderTemplate(w, data, "example.html")
}

View File

@ -1,14 +1,13 @@
package routes
import (
"GoStart/src/utils"
"net/http"
)
func RootHandler(w http.ResponseWriter, r *http.Request) {
data := PageData{
data := utils.PageData{
Title: "Home Page",
}
// Call renderTemplate with the specific template for this route
renderTemplate(w, data, "index.html")
utils.RenderTemplate(w, data, "index.html")
}

View File

@ -1,13 +1,13 @@
package routes
package utils
import (
"GoStart/src/utils"
"html/template"
"net/http"
)
type PageData struct {
Title string
Theme string
}
// Define the base directories for views and templates
@ -24,9 +24,9 @@ var templateFiles = []string{
}
// Initialize the common templates with full paths
var layout = utils.PrependDir(templatesDir, templateFiles)
var layout = PrependDir(templatesDir, templateFiles)
func renderTemplate(w http.ResponseWriter, data PageData, view string) {
func RenderTemplate(w http.ResponseWriter, data PageData, view string) {
// Append the specific template for the route
templates := append(layout, viewsDir+view)

View File

@ -1,6 +1,6 @@
{{define "layout"}}
<!DOCTYPE html>
<html lang="en" data-theme="">
<html lang="en" data-theme="{{.Theme}}">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />