defined template and view directory seperately
This commit is contained in:
parent
6f755be6cf
commit
e699eec6e0
7
main.go
7
main.go
@ -3,16 +3,15 @@ package main
|
||||
import (
|
||||
"GoStart/src/api"
|
||||
"GoStart/src/routes"
|
||||
"GoStart/src/util"
|
||||
"GoStart/src/utils"
|
||||
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
// Load Configurations
|
||||
cfg, err := util.LoadConfig()
|
||||
// Load Configurations
|
||||
cfg, err := utils.LoadConfig()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to load config: %v\n", err)
|
||||
return
|
||||
|
@ -10,5 +10,5 @@ func ExampleHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Call renderTemplate with the specific template for this route
|
||||
renderTemplate(w, data, "web/views/example.html")
|
||||
renderTemplate(w, data, "example.html")
|
||||
}
|
||||
|
@ -10,5 +10,5 @@ func RootHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Call renderTemplate with the specific template for this route
|
||||
renderTemplate(w, data, "web/views/index.html")
|
||||
renderTemplate(w, data, "index.html")
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"GoStart/src/utils"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
@ -9,16 +10,26 @@ type PageData struct {
|
||||
Title string
|
||||
}
|
||||
|
||||
// Define the base directories for views and templates
|
||||
const (
|
||||
viewsDir = "web/views/"
|
||||
templatesDir = "web/views/templates/"
|
||||
)
|
||||
|
||||
// Define the common layout templates filenames
|
||||
var layoutFiles = []string{
|
||||
"#layout.html",
|
||||
"header.html",
|
||||
"footer.html",
|
||||
}
|
||||
|
||||
// Initialize the common templates with full paths
|
||||
var layout = utils.PrependDir(templatesDir, layoutFiles)
|
||||
|
||||
func renderTemplate(w http.ResponseWriter, data PageData, view string) {
|
||||
// List of common templates
|
||||
layout := []string{
|
||||
"web/views/template/#layout.html",
|
||||
"web/views/template/#header.html",
|
||||
"web/views/template/#footer.html",
|
||||
}
|
||||
|
||||
// Append the specific template for the route
|
||||
templates := append(layout, view)
|
||||
templates := append(layout, viewsDir+view)
|
||||
|
||||
// Parse all templates
|
||||
tmpl, err := template.ParseFiles(templates...)
|
||||
|
@ -1,4 +1,4 @@
|
||||
package util
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@ -8,7 +8,7 @@ import (
|
||||
|
||||
type Config struct {
|
||||
Port int `json:"port"`
|
||||
Development string `json:"development"`
|
||||
Development string `json:"development"`
|
||||
}
|
||||
|
||||
func LoadConfig() (*Config, error) {
|
10
src/utils/helpers.go
Normal file
10
src/utils/helpers.go
Normal file
@ -0,0 +1,10 @@
|
||||
package utils
|
||||
|
||||
// Helper function to prepend a directory path to a list of filenames
|
||||
func PrependDir(dir string, files []string) []string {
|
||||
var fullPaths []string
|
||||
for _, file := range files {
|
||||
fullPaths = append(fullPaths, dir+file)
|
||||
}
|
||||
return fullPaths
|
||||
}
|
Loading…
Reference in New Issue
Block a user