Rework templates to be less hard-coded
This commit is contained in:
parent
a66e26477c
commit
02e1c80c24
7 changed files with 28 additions and 32 deletions
|
@ -14,7 +14,7 @@
|
||||||
<link rel="icon" type="image/x-icon" href="/public/favicon.ico">
|
<link rel="icon" type="image/x-icon" href="/public/favicon.ico">
|
||||||
<link rel="stylesheet" href="/public/main.css">
|
<link rel="stylesheet" href="/public/main.css">
|
||||||
<script src="/public/date.js"></script>
|
<script src="/public/date.js"></script>
|
||||||
<title>Hibiscus.txt</title>
|
<title>Hibiscus</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{{template "header" .}}
|
{{template "header" .}}
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
{{define "main"}}
|
|
||||||
<h2><label for="day">{{ .Date }}</label> | <a href="/day">Go back</a></h2>
|
|
||||||
<textarea id="day" cols="40" rows="15" readonly>{{ .Day }}</textarea>
|
|
||||||
{{end}}
|
|
7
pages/edit.html
Normal file
7
pages/edit.html
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
{{define "main"}}
|
||||||
|
<form method="POST">
|
||||||
|
<h2><label for="text">{{ .Title }}:</label></h2>
|
||||||
|
<textarea id="text" cols="40" rows="15" name="text">{{ .Content }}</textarea>
|
||||||
|
<button type="submit">Save</button>
|
||||||
|
</form>
|
||||||
|
{{end}}
|
4
pages/entry.html
Normal file
4
pages/entry.html
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{{define "main"}}
|
||||||
|
<h2><label for="text">{{ .Title }}</label> | <a href="/day">Go back</a></h2>
|
||||||
|
<textarea id="text" cols="40" rows="15" readonly>{{ .Content }}</textarea>
|
||||||
|
{{end}}
|
|
@ -1,7 +0,0 @@
|
||||||
{{define "main"}}
|
|
||||||
<form method="POST">
|
|
||||||
<h2><label for="day">Your day so far:</label></h2>
|
|
||||||
<textarea id="day" cols="40" rows="15" name="day">{{ .Day }}</textarea>
|
|
||||||
<button type="submit">Save</button>
|
|
||||||
</form>
|
|
||||||
{{end}}
|
|
|
@ -2,7 +2,7 @@
|
||||||
<h2>{{.Title}}</h2>
|
<h2>{{.Title}}</h2>
|
||||||
<ul>
|
<ul>
|
||||||
{{range .Entries}}
|
{{range .Entries}}
|
||||||
<li><a href="/day/{{.Link}}">{{.Name}}</a></li>
|
<li><a href="/{{.Link}}">{{.Title}}</a></li>
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
{{end}}
|
{{end}}
|
34
routes.go
34
routes.go
|
@ -10,19 +10,15 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DayData struct {
|
type EntryList struct {
|
||||||
Day string
|
|
||||||
Date string
|
|
||||||
}
|
|
||||||
|
|
||||||
type List struct {
|
|
||||||
Title string
|
Title string
|
||||||
Entries []ListEntry
|
Entries []Entry
|
||||||
}
|
}
|
||||||
|
|
||||||
type ListEntry struct {
|
type Entry struct {
|
||||||
Name string
|
Title string
|
||||||
Link string
|
Content string
|
||||||
|
Link string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotFound returns a user-friendly 404 error page
|
// NotFound returns a user-friendly 404 error page
|
||||||
|
@ -50,14 +46,14 @@ func GetToday(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
files := []string{"./pages/base.html", "./pages/index.html"}
|
files := []string{"./pages/base.html", "./pages/edit.html"}
|
||||||
ts, err := template.ParseFiles(files...)
|
ts, err := template.ParseFiles(files...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalError(w, r)
|
InternalError(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ts.ExecuteTemplate(w, "base", DayData{Day: string(day)})
|
err = ts.ExecuteTemplate(w, "base", Entry{Title: "Your day so far", Content: string(day)})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalError(w, r)
|
InternalError(w, r)
|
||||||
return
|
return
|
||||||
|
@ -66,7 +62,7 @@ func GetToday(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// PostToday saves today's entry from form and redirects back to GET
|
// PostToday saves today's entry from form and redirects back to GET
|
||||||
func PostToday(w http.ResponseWriter, r *http.Request) {
|
func PostToday(w http.ResponseWriter, r *http.Request) {
|
||||||
err := SaveToday([]byte(r.FormValue("day")))
|
err := SaveToday([]byte(r.FormValue("text")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("error saving today's file", "error", err)
|
slog.Error("error saving today's file", "error", err)
|
||||||
}
|
}
|
||||||
|
@ -81,7 +77,7 @@ func GetDays(w http.ResponseWriter, r *http.Request) {
|
||||||
InternalError(w, r)
|
InternalError(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var daysFormatted []ListEntry
|
var daysFormatted []Entry
|
||||||
for i, _ := range day {
|
for i, _ := range day {
|
||||||
v := day[len(day)-1-i] // This is suboptimal, but reverse order is better here
|
v := day[len(day)-1-i] // This is suboptimal, but reverse order is better here
|
||||||
dayString := v
|
dayString := v
|
||||||
|
@ -92,10 +88,10 @@ func GetDays(w http.ResponseWriter, r *http.Request) {
|
||||||
if v == time.Now().Format(time.DateOnly) {
|
if v == time.Now().Format(time.DateOnly) {
|
||||||
dayString = "Today"
|
dayString = "Today"
|
||||||
}
|
}
|
||||||
daysFormatted = append(daysFormatted, ListEntry{Name: dayString, Link: v})
|
daysFormatted = append(daysFormatted, Entry{Title: dayString, Link: "day/" + v})
|
||||||
}
|
}
|
||||||
|
|
||||||
files := []string{"./pages/base.html", "./pages/days.html"}
|
files := []string{"./pages/base.html", "./pages/list.html"}
|
||||||
ts, err := template.ParseFiles(files...)
|
ts, err := template.ParseFiles(files...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Error parsing template files", "error", err)
|
slog.Error("Error parsing template files", "error", err)
|
||||||
|
@ -103,7 +99,7 @@ func GetDays(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ts.ExecuteTemplate(w, "base", List{Title: "Previous days", Entries: daysFormatted})
|
err = ts.ExecuteTemplate(w, "base", EntryList{Title: "Previous days", Entries: daysFormatted})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Error executing template", "error", err)
|
slog.Error("Error executing template", "error", err)
|
||||||
InternalError(w, r)
|
InternalError(w, r)
|
||||||
|
@ -130,7 +126,7 @@ func GetDay(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
files := []string{"./pages/base.html", "./pages/day.html"}
|
files := []string{"./pages/base.html", "./pages/entry.html"}
|
||||||
ts, err := template.ParseFiles(files...)
|
ts, err := template.ParseFiles(files...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalError(w, r)
|
InternalError(w, r)
|
||||||
|
@ -142,7 +138,7 @@ func GetDay(w http.ResponseWriter, r *http.Request) {
|
||||||
dayString = t.Format("02 Jan 2006")
|
dayString = t.Format("02 Jan 2006")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ts.ExecuteTemplate(w, "base", DayData{Day: string(day), Date: dayString})
|
err = ts.ExecuteTemplate(w, "base", Entry{Content: string(day), Title: dayString})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalError(w, r)
|
InternalError(w, r)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue