Fix templates

This commit is contained in:
Jan-Lukas Else 2020-10-12 18:47:23 +02:00
parent d15799997f
commit fe5a1baab2
14 changed files with 116 additions and 106 deletions

View File

@ -6,16 +6,16 @@ import (
) )
type errorData struct { type errorData struct {
Blog string
Title string Title string
Message string Message string
} }
func serve404(w http.ResponseWriter, r *http.Request) { func serve404(w http.ResponseWriter, r *http.Request) {
render(w, templateError, &errorData{ render(w, templateError, &renderData{
Blog: appConfig.DefaultBlog, Data: &errorData{
Title: "404 Not Found", Title: "404 Not Found",
Message: fmt.Sprintf("`%s` was not found", r.RequestURI), Message: fmt.Sprintf("`%s` was not found", r.RequestURI),
},
}) })
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
} }

View File

@ -42,7 +42,10 @@ func servePost(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
render(w, templatePost, post) render(w, templatePost, &renderData{
blogString: post.Blog,
Data: post,
})
} }
type indexTemplateData struct { type indexTemplateData struct {
@ -108,14 +111,15 @@ func serveTaxonomy(blog string, tax *taxonomy) func(w http.ResponseWriter, r *ht
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
render(w, templateTaxonomy, struct { render(w, templateTaxonomy, &renderData{
Taxonomy *taxonomy blogString: blog,
TaxonomyValues []string Data: struct {
Blog string Taxonomy *taxonomy
}{ TaxonomyValues []string
Taxonomy: tax, }{
TaxonomyValues: allValues, Taxonomy: tax,
Blog: blog, TaxonomyValues: allValues,
},
}) })
} }
} }
@ -202,16 +206,18 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) {
if len(template) == 0 { if len(template) == 0 {
template = templateIndex template = templateIndex
} }
render(w, template, &indexTemplateData{ render(w, template, &renderData{
Blog: ic.blog, blogString: ic.blog,
Title: title, Data: &indexTemplateData{
Description: description, Title: title,
Posts: posts, Description: description,
HasPrev: p.HasPrev(), Posts: posts,
HasNext: p.HasNext(), HasPrev: p.HasPrev(),
First: ic.path, HasNext: p.HasNext(),
Prev: fmt.Sprintf("%s/page/%d", ic.path, prevPage), First: ic.path,
Next: fmt.Sprintf("%s/page/%d", ic.path, nextPage), Prev: fmt.Sprintf("%s/page/%d", ic.path, prevPage),
Next: fmt.Sprintf("%s/page/%d", ic.path, nextPage),
},
}) })
} }
} }

View File

@ -21,12 +21,8 @@ func serveRedirect(w http.ResponseWriter, r *http.Request) {
} }
// Send redirect // Send redirect
w.Header().Set("Location", redirect) w.Header().Set("Location", redirect)
render(w, templateRedirect, struct { render(w, templateRedirect, &renderData{
Blog string Data: redirect,
Permalink string
}{
Blog: appConfig.DefaultBlog,
Permalink: redirect,
}) })
w.WriteHeader(http.StatusFound) w.WriteHeader(http.StatusFound)
} }

View File

@ -30,17 +30,11 @@ var templateFunctions template.FuncMap
func initRendering() error { func initRendering() error {
templateFunctions = template.FuncMap{ templateFunctions = template.FuncMap{
"blogs": func() map[string]*configBlog {
return appConfig.Blogs
},
"blog": func(blog string) *configBlog {
return appConfig.Blogs[blog]
},
"micropub": func() *configMicropub { "micropub": func() *configMicropub {
return appConfig.Micropub return appConfig.Micropub
}, },
"menu": func(blog, id string) *menu { "menu": func(blog *configBlog, id string) *menu {
return appConfig.Blogs[blog].Menus[id] return blog.Menus[id]
}, },
"md": func(content string) template.HTML { "md": func(content string) template.HTML {
htmlContent, err := renderMarkdown(content) htmlContent, err := renderMarkdown(content)
@ -73,9 +67,12 @@ func initRendering() error {
}, },
"asset": assetFile, "asset": assetFile,
"string": getDefaultTemplateString, "string": getDefaultTemplateString,
"include": func(templateName string, data interface{}) (template.HTML, error) { "include": func(templateName string, blog *configBlog, data interface{}) (template.HTML, error) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := templates[templateName].ExecuteTemplate(buf, templateName, data) err := templates[templateName].ExecuteTemplate(buf, templateName, &renderData{
Blog: blog,
Data: data,
})
return template.HTML(buf.String()), err return template.HTML(buf.String()), err
}, },
"urlize": urlize, "urlize": urlize,
@ -103,7 +100,20 @@ func initRendering() error {
return nil return nil
} }
func render(w http.ResponseWriter, template string, data interface{}) { type renderData struct {
blogString string
Blog *configBlog
Data interface{}
}
func render(w http.ResponseWriter, template string, data *renderData) {
// Check render data
if data.Blog == nil {
if len(data.blogString) == 0 {
data.blogString = appConfig.DefaultBlog
}
data.Blog = appConfig.Blogs[data.blogString]
}
// We need to use a buffer here to enable minification // We need to use a buffer here to enable minification
var buffer bytes.Buffer var buffer bytes.Buffer
err := templates[template].ExecuteTemplate(&buffer, template, data) err := templates[template].ExecuteTemplate(&buffer, template, data)

View File

@ -1,14 +1,14 @@
{{ define "base" }} {{ define "base" }}
<!doctype html> <!doctype html>
<html lang={{ (blog .Blog).Lang }}> <html lang={{ .Blog.Lang }}>
<meta charset=utf-8> <meta charset=utf-8>
<meta name=viewport content="width=device-width,initial-scale=1"> <meta name=viewport content="width=device-width,initial-scale=1">
<meta http-equiv=x-ua-compatible content="IE=edge"> <meta http-equiv=x-ua-compatible content="IE=edge">
<link rel="stylesheet" href="{{ asset "css/styles.css" }}"> <link rel="stylesheet" href="{{ asset "css/styles.css" }}">
{{ template "title" . }} {{ template "title" . }}
{{ if micropub.Enabled }} {{ if micropub.Enabled }}
{{ include "micropub" . }} {{ include "micropub" .Blog .Data }}
{{ end }} {{ end }}
{{ include "header" . }} {{ include "header" .Blog .Data }}
{{ template "main" . }} {{ template "main" . }}
{{ end }} {{ end }}

View File

@ -1,11 +1,11 @@
{{ define "title" }} {{ define "title" }}
<title>{{ with .Title }}{{ . }}{{end}}</title> <title>{{ with .Data.Title }}{{ . }}{{end}}</title>
{{ end }} {{ end }}
{{ define "main" }} {{ define "main" }}
<main> <main>
{{ with .Title }}<h1>{{ . }}</h1>{{ end }} {{ with .Data.Title }}<h1>{{ . }}</h1>{{ end }}
{{ with .Message }}{{ md . }}{{ end }} {{ with .Data.Message }}{{ md . }}{{ end }}
</main> </main>
{{ end }} {{ end }}

View File

@ -1,8 +1,7 @@
{{ define "header" }} {{ define "header" }}
<header> <header>
{{ $blog := (blog .Blog) }} <h1><a href="/" rel="home" title="{{ .Blog.Title }}">{{ .Blog.Title }}</a></h1>
<h1><a href="/" rel="home" title="{{ $blog.Title }}">{{ $blog.Title }}</a></h1> {{ with .Blog.Description }}<p><i>{{ . }}</i></p>{{ end }}
{{ with $blog.Description }}<p><i>{{ . }}</i></p>{{ end }} {{ include "menu" .Blog .Data }}
{{ include "menu" . }}
</header> </header>
{{ end }} {{ end }}

View File

@ -1,38 +1,39 @@
{{ define "title" }} {{ define "title" }}
{{ if .Title }} {{ if .Data.Title }}
<title>{{ .Title }} - {{ (blog .Blog).Title }}</title> <title>{{ .Data.Title }} - {{ .Blog.Title }}</title>
{{ else }} {{ else }}
<title>{{ (blog .Blog).Title }}</title> <title>{{ .Blog.Title }}</title>
{{ end }} {{ end }}
<link rel="alternate" <link rel="alternate"
type="application/rss+xml" type="application/rss+xml"
title="RSS" title="RSS"
href="{{ .First }}.rss"/> href="{{ .Data.First }}.rss"/>
<link rel="alternate" <link rel="alternate"
type="application/atom+xml" type="application/atom+xml"
title="Atom" title="Atom"
href="{{ .First }}.atom"/> href="{{ .Data.First }}.atom"/>
<link rel="alternate" <link rel="alternate"
type="application/feed+json" type="application/feed+json"
title="JSON Feed" title="JSON Feed"
href="{{ .First }}.json"/> href="{{ .Data.First }}.json"/>
{{ end }} {{ end }}
{{ define "main" }} {{ define "main" }}
<main> <main>
{{ with .Title }}<h1>{{ . }}</h1>{{ end }} {{ with .Data.Title }}<h1>{{ . }}</h1>{{ end }}
{{ with .Description }}{{ md . }}{{ end }} {{ with .Data.Description }}{{ md . }}{{ end }}
{{ if (or .Title .Description) }} {{ if (or .Data.Title .Data.Description) }}
<hr> <hr>
{{ end }} {{ end }}
{{ range $i, $post := .Posts }} {{ $blog := .Blog }}
{{ include "summary" . }} {{ range $i, $post := .Data.Posts }}
{{ include "summary" $blog $post }}
{{ end }} {{ end }}
{{ if .HasPrev }} {{ if .Data.HasPrev }}
<p><a href="{{ .Prev }}">Prev</a></p> <p><a href="{{ .Data.Prev }}">Prev</a></p>
{{ end }} {{ end }}
{{ if .HasNext }} {{ if .Data.HasNext }}
<p><a href="{{ .Next }}">Next</a></p> <p><a href="{{ .Data.Next }}">Next</a></p>
{{ end }} {{ end }}
</main> </main>
{{ end }} {{ end }}

View File

@ -1,28 +1,27 @@
{{ define "title" }} {{ define "title" }}
{{ $blog := (blog .Blog) }} {{ if .Blog.Photos.Title }}
{{ if $blog.Photos.Title }} <title>{{ .Blog.Photos.Title }} - {{ .Blog.Title }}</title>
<title>{{ $blog.Photos.Title }} - {{ $blog.Title }}</title>
{{ else }} {{ else }}
<title>{{ $blog.Title }}</title> <title>{{ .Blog.Title }}</title>
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ define "main" }} {{ define "main" }}
<main> <main>
{{ $blog := (blog .Blog) }} {{ with .Blog.Photos.Title }}<h1>{{ . }}</h1>{{ end }}
{{ with $blog.Photos.Title }}<h1>{{ . }}</h1>{{ end }} {{ with .Blog.Photos.Description }}{{ md . }}{{ end }}
{{ with $blog.Photos.Description }}{{ md . }}{{ end }} {{ if (or .Blog.Photos.Title .Blog.Photos.Description) }}
{{ if (or $blog.Photos.Title $blog.Photos.Description) }}
<hr> <hr>
{{ end }} {{ end }}
{{ range $i, $post := .Posts }} {{ $blog := .Blog }}
{{ include "photosummary" . }} {{ range $i, $post := .Data.Posts }}
{{ include "photosummary" $blog $post }}
{{ end }} {{ end }}
{{ if .HasPrev }} {{ if .Data.HasPrev }}
<p><a href="{{ .Prev }}">Prev</a></p> <p><a href="{{ .Data.Prev }}">Prev</a></p>
{{ end }} {{ end }}
{{ if .HasNext }} {{ if .Data.HasNext }}
<p><a href="{{ .Next }}">Next</a></p> <p><a href="{{ .Data.Next }}">Next</a></p>
{{ end }} {{ end }}
</main> </main>
{{ end }} {{ end }}

View File

@ -1,12 +1,12 @@
{{ define "photosummary" }} {{ define "photosummary" }}
<article> <article>
{{ with p . "title" }}<h2>{{ . }}</h2>{{ end }} {{ with p .Data "title" }}<h2>{{ . }}</h2>{{ end }}
{{ with .Published }}<p>{{ dateformat . "02. Jan 2006" }}</p>{{ end }} {{ with .Data.Published }}<p>{{ dateformat . "02. Jan 2006" }}</p>{{ end }}
{{ range $i, $photo := ( ps . (blog .Blog).Photos.Parameter ) }} {{ range $i, $photo := ( ps .Data .Blog.Photos.Parameter ) }}
{{ md ( printf "![](%s)" $photo ) }} {{ md ( printf "![](%s)" $photo ) }}
{{ end }} {{ end }}
<p>{{ summary . }}</p> <p>{{ summary .Data }}</p>
<a href="{{ .Path }}">View</a> <a href="{{ .Data.Path }}">View</a>
</article> </article>
<hr> <hr>
{{ end }} {{ end }}

View File

@ -1,22 +1,21 @@
{{ define "title" }} {{ define "title" }}
<title>{{ with p . "title" }}{{ . }} - {{end}}{{ (blog .Blog).Title }}</title> <title>{{ with p .Data "title" }}{{ . }} - {{end}}{{ .Blog.Title }}</title>
{{ end }} {{ end }}
{{ define "main" }} {{ define "main" }}
<main class=h-entry> <main class=h-entry>
<article> <article>
{{ with title . }}<h1 class=p-name>{{ . }}</h1>{{ end }} {{ with title .Data }}<h1 class=p-name>{{ . }}</h1>{{ end }}
{{ with .Published }} {{ with .Data.Published }}
<p>{{ string "publishedon" }} {{ dateformat . "02. Jan 2006" }}</p>{{ end }} <p>{{ string "publishedon" }} {{ dateformat . "02. Jan 2006" }}</p>{{ end }}
{{ with .Updated }} {{ with .Data.Updated }}
<p>{{ string "updatedon" }} {{ dateformat . "02. Jan 2006" }}</p>{{ end }} <p>{{ string "updatedon" }} {{ dateformat . "02. Jan 2006" }}</p>{{ end }}
{{ with .Content }} {{ with .Data.Content }}
<div class=e-content>{{ md . }}</div> <div class=e-content>{{ md . }}</div>
{{ end }} {{ end }}
</article> </article>
{{ $taxonomies := (blog .Blog).Taxonomies }} {{ $post := .Data }}
{{ $post := . }} {{ range $i, $tax := .Blog.Taxonomies }}
{{ range $i, $tax := $taxonomies }}
{{ $tvs := ps $post $tax.Name }} {{ $tvs := ps $post $tax.Name }}
{{ if gt (len $tvs) 0 }} {{ if gt (len $tvs) 0 }}
<p>In <b>{{ $tax.Title }}</b>: <p>In <b>{{ $tax.Title }}</b>:

View File

@ -1,9 +1,9 @@
{{ define "redirect" }} {{ define "redirect" }}
<!doctype html> <!doctype html>
<html lang={{ (blog .Blog).Lang }}> <html lang={{ .Blog.Lang }}>
<meta charset="utf-8"/> <meta charset="utf-8"/>
<meta name="robots" content="noindex"> <meta name="robots" content="noindex">
<title>{{ .Permalink }}</title> <title>{{ .Data }}</title>
<link rel="canonical" href="{{ .Permalink }}"/> <link rel="canonical" href="{{ .Data }}"/>
<meta http-equiv="refresh" content="0; url={{ .Permalink }}"/> <meta http-equiv="refresh" content="0; url={{ .Data }}"/>
{{ end }} {{ end }}

View File

@ -1,9 +1,9 @@
{{ define "summary" }} {{ define "summary" }}
<article> <article>
{{ with p . "title" }}<h2>{{ . }}</h2>{{ end }} {{ with p .Data "title" }}<h2>{{ . }}</h2>{{ end }}
{{ with .Published }}<p>{{ dateformat . "02. Jan 2006" }}</p>{{ end }} {{ with .Data.Published }}<p>{{ dateformat . "02. Jan 2006" }}</p>{{ end }}
<p>{{ summary . }}</p> <p>{{ summary .Data }}</p>
<a href="{{ .Path }}">View</a> <a href="{{ .Data.Path }}">View</a>
</article> </article>
<hr> <hr>
{{ end }} {{ end }}

View File

@ -1,14 +1,14 @@
{{ define "title" }} {{ define "title" }}
<title>{{ .Taxonomy.Title }} - {{ (blog .Blog).Title }}</title> <title>{{ .Data.Taxonomy.Title }} - {{ .Blog.Title }}</title>
{{ end }} {{ end }}
{{ define "main" }} {{ define "main" }}
<main> <main>
{{ with .Taxonomy.Title }}<h1>{{ . }}</h1>{{ end }} {{ with .Data.Taxonomy.Title }}<h1>{{ . }}</h1>{{ end }}
{{ with .Taxonomy.Description }}{{ md . }}{{ end }} {{ with .Data.Taxonomy.Description }}{{ md . }}{{ end }}
<ul> <ul>
{{ $taxonomy := .Taxonomy.Name }} {{ $taxonomy := .Data.Taxonomy.Name }}
{{ range $i, $value := (sort .TaxonomyValues) }} {{ range $i, $value := (sort .Data.TaxonomyValues) }}
<li><a href="/{{ $taxonomy }}/{{ urlize . }}">{{ . }}</a></li> <li><a href="/{{ $taxonomy }}/{{ urlize . }}">{{ . }}</a></li>
{{ end }} {{ end }}
</ul> </ul>