Fix permalinks

This commit is contained in:
Adriano Caloiaro 2023-07-30 18:11:43 -06:00
parent e097cfd37d
commit c4f3ebf833
No known key found for this signature in database
GPG key ID: C2BC56DE73CE3F75
3 changed files with 20 additions and 12 deletions

View file

@ -1,6 +1,6 @@
<div class="posts">
{{range .Posts}}
{{ template "show.tmpl" (PostWrapper . $.UserID $.ApiBaseUrl) }}
{{ template "show.tmpl" (argsfn "Garbage" . "UserID" $.UserID "ApiBaseUrl" $.ApiBaseUrl "argsfn" argsfn) }}
{{end}}
</div>

View file

@ -3,7 +3,7 @@
<h1 class="post-title">
{{if .Url}}<a href={{ .Url }} target="_blank">{{.Title}} (link)</a>{{else}}{{.Title}}{{ end }}</h1>
<div class="post-meta">
{{ template "uplevel_button.tmpl" (PostWrapper . $.UserID $.ApiBaseUrl) }}
{{ template "uplevel_button.tmpl" (argsfn "Garbage" . "UserID" $.UserID "ApiBaseUrl" $.ApiBaseUrl) }}
<span>Submitter:&nbsp;{{ .Username }}</span>
{{ if eq .OwnerID.String $.UserID }}
<a href="{{ $.ApiBaseUrl }}/garbage/{{ .ID }}/edit"

View file

@ -660,15 +660,20 @@ func createAccountHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
// showParams creates a map that includes all ther required parameters for "garbage/show.tmpl"
// this is used in "garbage/list.html" to combine all the params for the show template inside
// the loop over all posts
func showParams(g Garbage, userID string, apiBaseURL string) map[string]any {
return map[string]any{
"Garbage": g,
"UserID": userID,
"ApiBaseUrl": apiBaseURL,
// argsfn is a template function to pass arbitrary template variables into sub-templates
func argsfn(kvs ...interface{}) (map[string]interface{}, error) {
if len(kvs)%2 != 0 {
return nil, errors.New("args requires even number of arguments")
}
m := make(map[string]interface{})
for i := 0; i < len(kvs); i += 2 {
s, ok := kvs[i].(string)
if !ok {
return nil, errors.New("even args to args must be strings")
}
m[s] = kvs[i+1]
}
return m, nil
}
// listGarbageHandler returns the latest garbage
@ -691,7 +696,7 @@ func listGarbageHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(
template.New("list.html").
Funcs(template.FuncMap{"PostWrapper": showParams}).
Funcs(template.FuncMap{"argsfn": argsfn}).
ParseFS(partialsFS, "partials/garbage/list.html", "partials/garbage/*.tmpl"))
err = tmpl.ExecuteTemplate(w, "list.html", map[string]any{
@ -730,7 +735,10 @@ func showGarbageHandler(w http.ResponseWriter, r *http.Request) {
}
var buff = bytes.NewBufferString("")
tmpl := template.Must(template.ParseFS(partialsFS, "partials/garbage/show.tmpl"))
tmpl := template.Must(
template.New("list.html").
Funcs(template.FuncMap{"argsfn": argsfn}).
ParseFS(partialsFS, "partials/garbage/show.tmpl", "partials/garbage/*.tmpl"))
err = tmpl.ExecuteTemplate(buff, "show.tmpl", map[string]any{
"Garbage": garbage,
"ApiBaseUrl": apiURL(),