mirror of
https://github.com/acaloiaro/garbagespeak.com
synced 2026-07-21 18:29:08 +00:00
Use sub-template for garbage list
This commit is contained in:
parent
10e8e23a05
commit
977eb926b5
5 changed files with 69 additions and 91 deletions
|
|
@ -1,47 +1,7 @@
|
|||
<div class="posts">
|
||||
{{range .Posts}}
|
||||
<article class="post on-list">
|
||||
<h1 class="post-title">{{ .Title }}</h1>
|
||||
<div class="post-meta">
|
||||
{{- with .Username -}}
|
||||
<span>Submitter: {{ . }}</span>
|
||||
{{- end -}}
|
||||
{{- with .CreatedAt -}}
|
||||
<time class="post-date">
|
||||
{{- .Format "2006-01-02" -}}
|
||||
</time>
|
||||
{{- end -}}
|
||||
|
||||
<a href="#"
|
||||
hx-get="{{ $.ApiBaseUrl }}/garbage/{{ .ID }}"
|
||||
hx-push-url="{{ $.ApiBaseUrl }}/garbage/{{ .ID }}"
|
||||
hx-target="#content">Link</a>
|
||||
|
||||
{{ if eq .OwnerID.String $.UserID }}
|
||||
<a href="#"
|
||||
hx-get="{{ $.ApiBaseUrl }}/garbage/{{ .ID }}/edit"
|
||||
hx-target="#content"
|
||||
hx-swap="outerHTML">Edit</a>
|
||||
{{ end }}
|
||||
</div>
|
||||
|
||||
<div class="post-content">
|
||||
<blockquote>{{ .Content }}</blockquote>
|
||||
</div>
|
||||
|
||||
<br/>
|
||||
<div class="post-meta">
|
||||
{{ if .Metadata.tags }}
|
||||
<span>tags</span>
|
||||
<span>
|
||||
{{ range .Metadata.tags }}
|
||||
{{- . -}},
|
||||
{{ end }}
|
||||
</span>
|
||||
{{ end }}
|
||||
</div>
|
||||
</article>
|
||||
{{end}}
|
||||
{{ template "show.tmpl" (PostWrapper . $.UserID $.ApiBaseUrl) }}
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
{{- with .Alert -}}
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
<div class="posts">
|
||||
{{ $apiBaseURL := .ApiBaseUrl }}
|
||||
{{ with .Garbage }}
|
||||
<article class="post on-list">
|
||||
<h1 class="post-title">{{ .Title }}</h1>
|
||||
<div class="post-meta">
|
||||
{{- with .Username -}}
|
||||
<span>Submitter: {{ . }}</span>
|
||||
{{- end -}}
|
||||
{{- with .CreatedAt -}}
|
||||
<time class="post-date">
|
||||
{{- .Format "2006-01-02" -}}
|
||||
</time>
|
||||
{{- end -}}
|
||||
{{if .Url}}<a href={{ .Url }}>Link</a>{{ end }}
|
||||
{{ if eq .OwnerID.String $.UserID }}
|
||||
<a href="#"
|
||||
hx-get="{{ $apiBaseURL }}/garbage/{{ .ID }}/edit"
|
||||
hx-target="#content"
|
||||
hx-swap="outerHTML">Edit</a>
|
||||
{{ end }}
|
||||
</div>
|
||||
|
||||
<div class="post-content">
|
||||
{{ .Content }}
|
||||
</div>
|
||||
|
||||
<br/>
|
||||
<div class="post-meta">
|
||||
{{ if .Metadata.tags }}
|
||||
<span>tags</span>
|
||||
<span>
|
||||
{{ range .Metadata.tags }}
|
||||
{{- . -}},
|
||||
{{ end }}
|
||||
</span>
|
||||
{{ end }}
|
||||
</div>
|
||||
</article>
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
{{- with .Alert -}}
|
||||
<div id="alert" remove-me="5s" class="alert">{{ . }}</div>
|
||||
{{ end }}
|
||||
48
partials/garbage/show.tmpl
Normal file
48
partials/garbage/show.tmpl
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
{{ with .Garbage }}
|
||||
<article class="post on-list">
|
||||
<h1 class="post-title">
|
||||
{{if .Url}}<a href={{ .Url }} target="_blank">{{.Title}} (link)</a>{{else}}{{.Title}}{{ end }}</h1>
|
||||
<div class="post-meta">
|
||||
{{- with .Username -}}
|
||||
<span>Submitter: {{ . }}</span>
|
||||
{{- end -}}
|
||||
{{ if eq .OwnerID.String $.UserID }}
|
||||
<a href="{{ $.ApiBaseUrl }}/garbage/{{ .ID }}/edit"
|
||||
hx-get="{{ $.ApiBaseUrl }}/garbage/{{ .ID }}/edit"
|
||||
hx-push-url="true"
|
||||
hx-target="#content"
|
||||
hx-swap="innerHTML">Edit</a>
|
||||
{{ end }}
|
||||
<a href="{{ $.ApiBaseUrl }}/garbage/{{ .ID }}"
|
||||
hx-get="{{ $.ApiBaseUrl }}/garbage/{{ .ID }}"
|
||||
hx-push-url="{{ $.ApiBaseUrl }}/garbage/{{ .ID }}"
|
||||
hx-target="#content"
|
||||
hx-swap="innerHTML">Permalink</a>
|
||||
|
||||
{{- with .CreatedAt -}}
|
||||
<time class="post-date">
|
||||
{{- .Format "2006-01-02" -}}
|
||||
</time>
|
||||
{{- end -}}
|
||||
</div>
|
||||
<div class="post-content">
|
||||
<blockquote>{{ .Content }}</blockquote>
|
||||
</div>
|
||||
|
||||
<br/>
|
||||
<div class="post-meta">
|
||||
{{ if .Metadata.tags }}
|
||||
<span>tags</span>
|
||||
<span>
|
||||
{{ range .Metadata.tags }}
|
||||
{{- . -}},
|
||||
{{ end }}
|
||||
</span>
|
||||
{{ end }}
|
||||
</div>
|
||||
</article>
|
||||
{{end}}
|
||||
|
||||
{{- with .Alert -}}
|
||||
<div id="alert" remove-me="5s" class="alert">{{ . }}</div>
|
||||
{{ end }}
|
||||
|
|
@ -1,2 +1,2 @@
|
|||
<li><a href="/garbage/new">Submit</a></li>
|
||||
<li><a href="/garbage/new/">Submit</a></li>
|
||||
<li><a href="{{ .ApiURL }}/users/logout">Log Out</a></li>
|
||||
|
|
|
|||
21
server.go
21
server.go
|
|
@ -616,6 +616,17 @@ 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,
|
||||
}
|
||||
}
|
||||
|
||||
// listGarbageHandler returns the latest garbage
|
||||
func listGarbageHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID := sessions.GetString(r.Context(), "userID")
|
||||
|
|
@ -634,7 +645,11 @@ func listGarbageHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.ParseFS(partialsFS, "partials/garbage/list.html"))
|
||||
tmpl := template.Must(
|
||||
template.New("list.html").
|
||||
Funcs(template.FuncMap{"PostWrapper": showParams}).
|
||||
ParseFS(partialsFS, "partials/garbage/list.html", "partials/garbage/show.tmpl"))
|
||||
|
||||
err = tmpl.ExecuteTemplate(w, "list.html", map[string]any{
|
||||
"Posts": garbage,
|
||||
"ApiBaseUrl": apiURL(),
|
||||
|
|
@ -671,8 +686,8 @@ func showGarbageHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
var buff = bytes.NewBufferString("")
|
||||
tmpl := template.Must(template.ParseFS(partialsFS, "partials/garbage/show.html"))
|
||||
err = tmpl.ExecuteTemplate(buff, "show.html", map[string]any{
|
||||
tmpl := template.Must(template.ParseFS(partialsFS, "partials/garbage/show.tmpl"))
|
||||
err = tmpl.ExecuteTemplate(buff, "show.tmpl", map[string]any{
|
||||
"Garbage": garbage,
|
||||
"ApiBaseUrl": apiURL(),
|
||||
"LoggedIn": isLoggedIn(r),
|
||||
|
|
|
|||
Loading…
Reference in a new issue