Skip to content

Commit

Permalink
added data models for templates and added a placeholder in about.page…
Browse files Browse the repository at this point in the history
….gohtml template
  • Loading branch information
flashl1ght committed Mar 10, 2024
1 parent eb63c5c commit 61e9a45
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
12 changes: 10 additions & 2 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/flashl1ght/myFirstGoApp/pkg/config"
"github.com/flashl1ght/myFirstGoApp/pkg/models"
"github.com/flashl1ght/myFirstGoApp/pkg/render"
)

Expand All @@ -29,10 +30,17 @@ func NewHandlers(r *Repository) {

// Home is the home page handler
func (m *Repository) Home(w http.ResponseWriter, r *http.Request) {
render.RenderTemplate(w, "home.page.gohtml")
render.RenderTemplate(w, "home.page.gohtml", &models.TemplateData{})
}

// About is the about page handler
func (m *Repository) About(w http.ResponseWriter, r *http.Request) {
render.RenderTemplate(w, "about.page.gohtml")
// perform some placeholder logic
stringMap := make(map[string]string)
stringMap["message"] = "This site is under construction"

// send the data to the template
render.RenderTemplate(w, "about.page.gohtml", &models.TemplateData{
StringMap: stringMap,
})
}
13 changes: 13 additions & 0 deletions pkg/models/templatedata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package models

// TemplateData holds data sent from handlers to templates
type TemplateData struct {
StringMap map[string]string
IntMap map[string]string
FloatMap map[string]float64
Data map[string]interface{}
CSRFToken string
Flash string
Warning string
Error string
}
12 changes: 10 additions & 2 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"

"github.com/flashl1ght/myFirstGoApp/pkg/config"
"github.com/flashl1ght/myFirstGoApp/pkg/models"
)

var app *config.AppConfig
Expand All @@ -17,8 +18,13 @@ func NewTemplates(a *config.AppConfig) {
app = a
}

// AddDefaultData adds data to TemplateData
func AddDefaultData(td *models.TemplateData) *models.TemplateData {
return td
}

// RenderTemplate renders templates using html/template
func RenderTemplate(w http.ResponseWriter, tmpl string) {
func RenderTemplate(w http.ResponseWriter, tmpl string, td *models.TemplateData) {
// get the template cache frokm the app config
templateCache := app.TemplateCache

Expand All @@ -29,7 +35,9 @@ func RenderTemplate(w http.ResponseWriter, tmpl string) {

buffer := new(bytes.Buffer)

_ = t.Execute(buffer, nil)
td = AddDefaultData(td)

_ = t.Execute(buffer, td)

//render the template
_, err := buffer.WriteTo(w)
Expand Down
2 changes: 1 addition & 1 deletion templates/about.page.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="row">
<div class="col">
<h1>This is the about page</h1>
<p>This is empty</p>
<p>{{index .StringMap "message"}}</p>
</div>
</div>
</div>
Expand Down

0 comments on commit 61e9a45

Please sign in to comment.