feat: add render duration in footer
This commit is contained in:
parent
d24f48bc52
commit
c05b34f4cd
5 changed files with 44 additions and 6 deletions
|
@ -2,16 +2,16 @@ body {
|
|||
margin: 0;
|
||||
}
|
||||
|
||||
header {
|
||||
body > header {
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
header > h1 {
|
||||
body > header > h1 {
|
||||
margin: 0;
|
||||
font-size: 1.2em;
|
||||
}
|
||||
|
||||
header ul {
|
||||
body > header ul {
|
||||
list-style: none;
|
||||
display: flex;
|
||||
gap: 5px;
|
||||
|
@ -19,6 +19,15 @@ header ul {
|
|||
padding: 5px;
|
||||
}
|
||||
|
||||
body > footer {
|
||||
font-size: 0.9em;
|
||||
margin-top: 10px;
|
||||
padding: 10px;
|
||||
display: flex;
|
||||
gap: 5px;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
#items {
|
||||
list-style: none;
|
||||
padding-left: 0;
|
||||
|
|
25
templates.go
25
templates.go
|
@ -5,8 +5,13 @@ import (
|
|||
"io"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
func getRenderDuration(start time.Time) time.Duration {
|
||||
return time.Now().Sub(start)
|
||||
}
|
||||
|
||||
func Render(w io.Writer, templateName string, data interface{}) error {
|
||||
asTemplatePath := func(templateName string) string {
|
||||
return filepath.Join("templates", fmt.Sprintf("%s.html.tmpl", templateName))
|
||||
|
@ -15,13 +20,29 @@ func Render(w io.Writer, templateName string, data interface{}) error {
|
|||
templatePartials := []string{
|
||||
asTemplatePath(templateName),
|
||||
asTemplatePath("base"),
|
||||
asTemplatePath("footer"),
|
||||
}
|
||||
|
||||
tmpl, err := template.New(templateName).ParseFiles(templatePartials...)
|
||||
start := time.Now()
|
||||
|
||||
tmpl := template.New(templateName).Funcs(template.FuncMap{
|
||||
"getRenderDuration": getRenderDuration,
|
||||
})
|
||||
|
||||
tmpl, err := tmpl.ParseFiles(templatePartials...)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tmpl.ExecuteTemplate(w, "base", data)
|
||||
type RenderMeta struct {
|
||||
RenderStart time.Time
|
||||
}
|
||||
|
||||
type RenderContext struct {
|
||||
Data interface{}
|
||||
Meta RenderMeta
|
||||
}
|
||||
|
||||
return tmpl.ExecuteTemplate(w, "base", RenderContext{Data: data, Meta: RenderMeta{RenderStart: start}})
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
</nav>
|
||||
</header>
|
||||
{{ template "content" . }}
|
||||
{{ template "footer" . }}
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
||||
|
|
7
templates/footer.html.tmpl
Normal file
7
templates/footer.html.tmpl
Normal file
|
@ -0,0 +1,7 @@
|
|||
{{ define "footer" }}
|
||||
<footer>
|
||||
<span>Morning Coffee</span>
|
||||
<span>☕</span>
|
||||
<span>Rendered: {{ getRenderDuration .Meta.RenderStart }}</span>
|
||||
</footer>
|
||||
{{ end }}
|
|
@ -1,6 +1,6 @@
|
|||
{{ define "content" }}
|
||||
<ul id="items">
|
||||
{{ range . }}
|
||||
{{ range .Data }}
|
||||
<li>
|
||||
<a href="{{ .Url }}">{{ .Title }}</a>
|
||||
<span>{{ .PublishedDate }}</span>
|
||||
|
|
Loading…
Reference in a new issue