Move item template to its own file

This commit is contained in:
Tim Van Baak 2025-01-27 19:35:13 -08:00
parent 1057b54b3d
commit 565522535f
3 changed files with 53 additions and 54 deletions

View File

@ -1,55 +1,5 @@
{{ define "title" }}{{ if .Items }}({{ len .Items }}) {{ end }}Intake{{ end }} {{ define "title" }}{{ if .Items }}({{ len .Items }}) {{ end }}Intake{{ end }}
{{ define "buttons" -}}
<button class="item-button" title="Deactivate {{ .Source }}/{{ .Id }}">&#10005;</button>
<button class="item-button" title="Punt {{ .Source }}/{{ .Id }}">&#8631;</button>
{{- if .Link }}<a class="item-link" href="{{ .Link }}" target="_blank">&#8663;</a>
{{ end -}}
{{ end }}
{{ define "item-title" -}}
<span class="item-title">{{ or .Title .Id | raw }}</span>
{{- end }}
{{ define "item" -}}
<article id="{{ .Source }}-{{ .Id }}">
{{/* The item title is a clickable <summary> if there is body content */}}
{{ if .Body }}
<details>
<summary>
{{ template "buttons" . }}
{{ template "item-title" . }}
</summary>
{{ if .Body }}
<p>{{ raw .Body }}</p>
{{ end }}
</details>
{{ template "buttons" . }}
{{ else }}
{{ template "buttons" . }}
{{ template "item-title" . }}<br>
{{ end }}
{{/* end if .Body */}}
{{/* author/time footer line */}}
{{ if or .Author .Time }}
<span class="item-info">
{{ .Author }}
{{ .Time | tsToDate }}
</span><br>
{{ end }}
{{/* source/id/created footer line */}}
<span class="item-info">
{{ .Source }}/{{ .Id }}
{{ .Created | tsToDate }}
</span>
</article>
{{ end }}
{{/* end define "item" */}}
{{ define "content" -}} {{ define "content" -}}
<article class="center"> <article class="center">
<span class="item-title"> <span class="item-title">
@ -67,7 +17,6 @@
<button>Deactivate All</button> <button>Deactivate All</button>
</article> </article>
{{/* if .Items */}}
{{ else }} {{ else }}
<article class="center"> <article class="center">
<span class="item-title">Feed is empty</span> <span class="item-title">Feed is empty</span>

View File

@ -29,8 +29,9 @@ var Stylesheet []byte
//go:embed *.html //go:embed *.html
var templates embed.FS var templates embed.FS
func load(file string) *template.Template { func load(files ...string) *template.Template {
return template.Must(template.New("layout.html").Funcs(funcs).ParseFS(templates, "layout.html", file)) files = append([]string{"layout.html"}, files...)
return template.Must(template.New("layout.html").Funcs(funcs).ParseFS(templates, files...))
} }
var home = load("home.html") var home = load("home.html")
@ -47,7 +48,7 @@ func Home(writer io.Writer, data HomeData) error {
return home.Execute(writer, data) return home.Execute(writer, data)
} }
var feed = load("feed.html") var feed = load("feed.html", "item.html")
type FeedData struct { type FeedData struct {
Items []core.Item Items []core.Item

49
web/html/item.html Normal file
View File

@ -0,0 +1,49 @@
{{ define "buttons" -}}
<button class="item-button" title="Deactivate {{ .Source }}/{{ .Id }}">&#10005;</button>
<button class="item-button" title="Punt {{ .Source }}/{{ .Id }}">&#8631;</button>
{{- if .Link }}<a class="item-link" href="{{ .Link }}" target="_blank">&#8663;</a>
{{ end -}}
{{ end }}
{{ define "item-title" -}}
<span class="item-title">{{ or .Title .Id | raw }}</span>
{{- end }}
{{ define "item" -}}
<article id="{{ .Source }}-{{ .Id }}">
{{/* The item title is a clickable <summary> if there is body content */}}
{{ if .Body }}
<details>
<summary>
{{ template "buttons" . }}
{{ template "item-title" . }}
</summary>
{{ if .Body }}
<p>{{ raw .Body }}</p>
{{ end }}
</details>
{{ template "buttons" . }}
{{ else }}
{{ template "buttons" . }}
{{ template "item-title" . }}<br>
{{ end }}
{{/* end if .Body */}}
{{/* author/time footer line */}}
{{ if or .Author .Time }}
<span class="item-info">
{{ .Author }}
{{ .Time | tsToDate }}
</span><br>
{{ end }}
{{/* source/id/created footer line */}}
<span class="item-info">
{{ .Source }}/{{ .Id }}
{{ .Created | tsToDate }}
</span>
</article>
{{ end }}
{{/* end define "item" */}}