Fix blank page: URL types and inline style issues in templates
Build and Deploy / build-and-deploy (push) Successful in 1m3s
Build and Deploy / build-and-deploy (push) Successful in 1m3s
- Use template.URL for all URLs passed to templates (RSSURL, ImageURL) - Pre-compute episode image URLs in handler as safe types - Remove all inline style attributes, use CSS classes instead - Add template error logging to catch silent failures - HTML/template rejects raw strings in URL and CSS+URL contexts
This commit is contained in:
+37
-9
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -40,20 +41,47 @@ func (h *EpisodeHandler) ServeDashboard(w http.ResponseWriter, r *http.Request)
|
||||
if title == "" {
|
||||
title = "Podstalk"
|
||||
}
|
||||
type epView struct {
|
||||
ID int64
|
||||
Title string
|
||||
Description string
|
||||
AudioPath string
|
||||
ImageURL template.URL
|
||||
PublishedAt time.Time
|
||||
}
|
||||
var epViews []epView
|
||||
for _, ep := range episodes {
|
||||
ev := epView{
|
||||
ID: ep.ID,
|
||||
Title: ep.Title,
|
||||
Description: ep.Description,
|
||||
AudioPath: ep.AudioPath,
|
||||
PublishedAt: ep.PublishedAt,
|
||||
}
|
||||
if ep.ImagePath != "" {
|
||||
ev.ImageURL = template.URL("/uploads/" + ep.ImagePath)
|
||||
} else if user.PodcastImage != "" {
|
||||
ev.ImageURL = template.URL("/uploads/" + user.PodcastImage)
|
||||
}
|
||||
epViews = append(epViews, ev)
|
||||
}
|
||||
|
||||
podcastImageURL := template.URL("")
|
||||
if user.PodcastImage != "" {
|
||||
podcastImageURL = template.URL("/uploads/" + user.PodcastImage)
|
||||
}
|
||||
|
||||
h.Tpl.Render(w, "dashboard", map[string]any{
|
||||
"Episodes": episodes,
|
||||
"PodcastTitle": title,
|
||||
"PodcastSlug": user.PodcastSlug,
|
||||
"PodcastImage": user.PodcastImage,
|
||||
"PodcastAuthor": user.PodcastAuthor,
|
||||
"BaseURL": h.BaseURL,
|
||||
"UserID": user.ID,
|
||||
"Episodes": epViews,
|
||||
"PodcastTitle": title,
|
||||
"PodcastSlug": user.PodcastSlug,
|
||||
"PodcastImageURL": podcastImageURL,
|
||||
"PodcastAuthor": user.PodcastAuthor,
|
||||
"RSSURL": template.URL(h.BaseURL + "/rss/" + user.PodcastSlug),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *EpisodeHandler) ServeNew(w http.ResponseWriter, r *http.Request) {
|
||||
userID := UserIDFromContext(r)
|
||||
|
||||
if r.Method == http.MethodGet {
|
||||
h.Tpl.Render(w, "episode_form", map[string]any{"Editing": false})
|
||||
return
|
||||
|
||||
+4
-2
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Templates struct {
|
||||
@@ -12,7 +13,8 @@ type Templates struct {
|
||||
func NewTemplates(tmpl *template.Template) *Templates {
|
||||
return &Templates{tmpl: tmpl}
|
||||
}
|
||||
|
||||
func (t *Templates) Render(w io.Writer, name string, data any) {
|
||||
_ = t.tmpl.ExecuteTemplate(w, name+".html", data)
|
||||
if err := t.tmpl.ExecuteTemplate(w, name+".html", data); err != nil {
|
||||
log.Printf("template error: %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user