mirror of https://github.com/jlelse/GoBlog
Fix templates
This commit is contained in:
parent
d15799997f
commit
fe5a1baab2
10
errors.go
10
errors.go
|
@ -6,16 +6,16 @@ import (
|
|||
)
|
||||
|
||||
type errorData struct {
|
||||
Blog string
|
||||
Title string
|
||||
Message string
|
||||
}
|
||||
|
||||
func serve404(w http.ResponseWriter, r *http.Request) {
|
||||
render(w, templateError, &errorData{
|
||||
Blog: appConfig.DefaultBlog,
|
||||
Title: "404 Not Found",
|
||||
Message: fmt.Sprintf("`%s` was not found", r.RequestURI),
|
||||
render(w, templateError, &renderData{
|
||||
Data: &errorData{
|
||||
Title: "404 Not Found",
|
||||
Message: fmt.Sprintf("`%s` was not found", r.RequestURI),
|
||||
},
|
||||
})
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
|
|
44
posts.go
44
posts.go
|
@ -42,7 +42,10 @@ func servePost(w http.ResponseWriter, r *http.Request) {
|
|||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
render(w, templatePost, post)
|
||||
render(w, templatePost, &renderData{
|
||||
blogString: post.Blog,
|
||||
Data: post,
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
render(w, templateTaxonomy, struct {
|
||||
Taxonomy *taxonomy
|
||||
TaxonomyValues []string
|
||||
Blog string
|
||||
}{
|
||||
Taxonomy: tax,
|
||||
TaxonomyValues: allValues,
|
||||
Blog: blog,
|
||||
render(w, templateTaxonomy, &renderData{
|
||||
blogString: blog,
|
||||
Data: struct {
|
||||
Taxonomy *taxonomy
|
||||
TaxonomyValues []string
|
||||
}{
|
||||
Taxonomy: tax,
|
||||
TaxonomyValues: allValues,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -202,16 +206,18 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) {
|
|||
if len(template) == 0 {
|
||||
template = templateIndex
|
||||
}
|
||||
render(w, template, &indexTemplateData{
|
||||
Blog: ic.blog,
|
||||
Title: title,
|
||||
Description: description,
|
||||
Posts: posts,
|
||||
HasPrev: p.HasPrev(),
|
||||
HasNext: p.HasNext(),
|
||||
First: ic.path,
|
||||
Prev: fmt.Sprintf("%s/page/%d", ic.path, prevPage),
|
||||
Next: fmt.Sprintf("%s/page/%d", ic.path, nextPage),
|
||||
render(w, template, &renderData{
|
||||
blogString: ic.blog,
|
||||
Data: &indexTemplateData{
|
||||
Title: title,
|
||||
Description: description,
|
||||
Posts: posts,
|
||||
HasPrev: p.HasPrev(),
|
||||
HasNext: p.HasNext(),
|
||||
First: ic.path,
|
||||
Prev: fmt.Sprintf("%s/page/%d", ic.path, prevPage),
|
||||
Next: fmt.Sprintf("%s/page/%d", ic.path, nextPage),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,12 +21,8 @@ func serveRedirect(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
// Send redirect
|
||||
w.Header().Set("Location", redirect)
|
||||
render(w, templateRedirect, struct {
|
||||
Blog string
|
||||
Permalink string
|
||||
}{
|
||||
Blog: appConfig.DefaultBlog,
|
||||
Permalink: redirect,
|
||||
render(w, templateRedirect, &renderData{
|
||||
Data: redirect,
|
||||
})
|
||||
w.WriteHeader(http.StatusFound)
|
||||
}
|
||||
|
|
32
render.go
32
render.go
|
@ -30,17 +30,11 @@ var templateFunctions template.FuncMap
|
|||
|
||||
func initRendering() error {
|
||||
templateFunctions = template.FuncMap{
|
||||
"blogs": func() map[string]*configBlog {
|
||||
return appConfig.Blogs
|
||||
},
|
||||
"blog": func(blog string) *configBlog {
|
||||
return appConfig.Blogs[blog]
|
||||
},
|
||||
"micropub": func() *configMicropub {
|
||||
return appConfig.Micropub
|
||||
},
|
||||
"menu": func(blog, id string) *menu {
|
||||
return appConfig.Blogs[blog].Menus[id]
|
||||
"menu": func(blog *configBlog, id string) *menu {
|
||||
return blog.Menus[id]
|
||||
},
|
||||
"md": func(content string) template.HTML {
|
||||
htmlContent, err := renderMarkdown(content)
|
||||
|
@ -73,9 +67,12 @@ func initRendering() error {
|
|||
},
|
||||
"asset": assetFile,
|
||||
"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)
|
||||
err := templates[templateName].ExecuteTemplate(buf, templateName, data)
|
||||
err := templates[templateName].ExecuteTemplate(buf, templateName, &renderData{
|
||||
Blog: blog,
|
||||
Data: data,
|
||||
})
|
||||
return template.HTML(buf.String()), err
|
||||
},
|
||||
"urlize": urlize,
|
||||
|
@ -103,7 +100,20 @@ func initRendering() error {
|
|||
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
|
||||
var buffer bytes.Buffer
|
||||
err := templates[template].ExecuteTemplate(&buffer, template, data)
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
{{ define "base" }}
|
||||
<!doctype html>
|
||||
<html lang={{ (blog .Blog).Lang }}>
|
||||
<html lang={{ .Blog.Lang }}>
|
||||
<meta charset=utf-8>
|
||||
<meta name=viewport content="width=device-width,initial-scale=1">
|
||||
<meta http-equiv=x-ua-compatible content="IE=edge">
|
||||
<link rel="stylesheet" href="{{ asset "css/styles.css" }}">
|
||||
{{ template "title" . }}
|
||||
{{ if micropub.Enabled }}
|
||||
{{ include "micropub" . }}
|
||||
{{ include "micropub" .Blog .Data }}
|
||||
{{ end }}
|
||||
{{ include "header" . }}
|
||||
{{ include "header" .Blog .Data }}
|
||||
{{ template "main" . }}
|
||||
{{ end }}
|
|
@ -1,11 +1,11 @@
|
|||
{{ define "title" }}
|
||||
<title>{{ with .Title }}{{ . }}{{end}}</title>
|
||||
<title>{{ with .Data.Title }}{{ . }}{{end}}</title>
|
||||
{{ end }}
|
||||
|
||||
{{ define "main" }}
|
||||
<main>
|
||||
{{ with .Title }}<h1>{{ . }}</h1>{{ end }}
|
||||
{{ with .Message }}{{ md . }}{{ end }}
|
||||
{{ with .Data.Title }}<h1>{{ . }}</h1>{{ end }}
|
||||
{{ with .Data.Message }}{{ md . }}{{ end }}
|
||||
</main>
|
||||
{{ end }}
|
||||
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
{{ define "header" }}
|
||||
<header>
|
||||
{{ $blog := (blog .Blog) }}
|
||||
<h1><a href="/" rel="home" title="{{ $blog.Title }}">{{ $blog.Title }}</a></h1>
|
||||
{{ with $blog.Description }}<p><i>{{ . }}</i></p>{{ end }}
|
||||
{{ include "menu" . }}
|
||||
<h1><a href="/" rel="home" title="{{ .Blog.Title }}">{{ .Blog.Title }}</a></h1>
|
||||
{{ with .Blog.Description }}<p><i>{{ . }}</i></p>{{ end }}
|
||||
{{ include "menu" .Blog .Data }}
|
||||
</header>
|
||||
{{ end }}
|
|
@ -1,38 +1,39 @@
|
|||
{{ define "title" }}
|
||||
{{ if .Title }}
|
||||
<title>{{ .Title }} - {{ (blog .Blog).Title }}</title>
|
||||
{{ if .Data.Title }}
|
||||
<title>{{ .Data.Title }} - {{ .Blog.Title }}</title>
|
||||
{{ else }}
|
||||
<title>{{ (blog .Blog).Title }}</title>
|
||||
<title>{{ .Blog.Title }}</title>
|
||||
{{ end }}
|
||||
<link rel="alternate"
|
||||
type="application/rss+xml"
|
||||
title="RSS"
|
||||
href="{{ .First }}.rss"/>
|
||||
href="{{ .Data.First }}.rss"/>
|
||||
<link rel="alternate"
|
||||
type="application/atom+xml"
|
||||
title="Atom"
|
||||
href="{{ .First }}.atom"/>
|
||||
href="{{ .Data.First }}.atom"/>
|
||||
<link rel="alternate"
|
||||
type="application/feed+json"
|
||||
title="JSON Feed"
|
||||
href="{{ .First }}.json"/>
|
||||
href="{{ .Data.First }}.json"/>
|
||||
{{ end }}
|
||||
|
||||
{{ define "main" }}
|
||||
<main>
|
||||
{{ with .Title }}<h1>{{ . }}</h1>{{ end }}
|
||||
{{ with .Description }}{{ md . }}{{ end }}
|
||||
{{ if (or .Title .Description) }}
|
||||
{{ with .Data.Title }}<h1>{{ . }}</h1>{{ end }}
|
||||
{{ with .Data.Description }}{{ md . }}{{ end }}
|
||||
{{ if (or .Data.Title .Data.Description) }}
|
||||
<hr>
|
||||
{{ end }}
|
||||
{{ range $i, $post := .Posts }}
|
||||
{{ include "summary" . }}
|
||||
{{ $blog := .Blog }}
|
||||
{{ range $i, $post := .Data.Posts }}
|
||||
{{ include "summary" $blog $post }}
|
||||
{{ end }}
|
||||
{{ if .HasPrev }}
|
||||
<p><a href="{{ .Prev }}">Prev</a></p>
|
||||
{{ if .Data.HasPrev }}
|
||||
<p><a href="{{ .Data.Prev }}">Prev</a></p>
|
||||
{{ end }}
|
||||
{{ if .HasNext }}
|
||||
<p><a href="{{ .Next }}">Next</a></p>
|
||||
{{ if .Data.HasNext }}
|
||||
<p><a href="{{ .Data.Next }}">Next</a></p>
|
||||
{{ end }}
|
||||
</main>
|
||||
{{ end }}
|
||||
|
|
|
@ -1,28 +1,27 @@
|
|||
{{ define "title" }}
|
||||
{{ $blog := (blog .Blog) }}
|
||||
{{ if $blog.Photos.Title }}
|
||||
<title>{{ $blog.Photos.Title }} - {{ $blog.Title }}</title>
|
||||
{{ if .Blog.Photos.Title }}
|
||||
<title>{{ .Blog.Photos.Title }} - {{ .Blog.Title }}</title>
|
||||
{{ else }}
|
||||
<title>{{ $blog.Title }}</title>
|
||||
<title>{{ .Blog.Title }}</title>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ define "main" }}
|
||||
<main>
|
||||
{{ $blog := (blog .Blog) }}
|
||||
{{ with $blog.Photos.Title }}<h1>{{ . }}</h1>{{ end }}
|
||||
{{ with $blog.Photos.Description }}{{ md . }}{{ end }}
|
||||
{{ if (or $blog.Photos.Title $blog.Photos.Description) }}
|
||||
{{ with .Blog.Photos.Title }}<h1>{{ . }}</h1>{{ end }}
|
||||
{{ with .Blog.Photos.Description }}{{ md . }}{{ end }}
|
||||
{{ if (or .Blog.Photos.Title .Blog.Photos.Description) }}
|
||||
<hr>
|
||||
{{ end }}
|
||||
{{ range $i, $post := .Posts }}
|
||||
{{ include "photosummary" . }}
|
||||
{{ $blog := .Blog }}
|
||||
{{ range $i, $post := .Data.Posts }}
|
||||
{{ include "photosummary" $blog $post }}
|
||||
{{ end }}
|
||||
{{ if .HasPrev }}
|
||||
<p><a href="{{ .Prev }}">Prev</a></p>
|
||||
{{ if .Data.HasPrev }}
|
||||
<p><a href="{{ .Data.Prev }}">Prev</a></p>
|
||||
{{ end }}
|
||||
{{ if .HasNext }}
|
||||
<p><a href="{{ .Next }}">Next</a></p>
|
||||
{{ if .Data.HasNext }}
|
||||
<p><a href="{{ .Data.Next }}">Next</a></p>
|
||||
{{ end }}
|
||||
</main>
|
||||
{{ end }}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
{{ define "photosummary" }}
|
||||
<article>
|
||||
{{ with p . "title" }}<h2>{{ . }}</h2>{{ end }}
|
||||
{{ with .Published }}<p>{{ dateformat . "02. Jan 2006" }}</p>{{ end }}
|
||||
{{ range $i, $photo := ( ps . (blog .Blog).Photos.Parameter ) }}
|
||||
{{ with p .Data "title" }}<h2>{{ . }}</h2>{{ end }}
|
||||
{{ with .Data.Published }}<p>{{ dateformat . "02. Jan 2006" }}</p>{{ end }}
|
||||
{{ range $i, $photo := ( ps .Data .Blog.Photos.Parameter ) }}
|
||||
{{ md ( printf "![](%s)" $photo ) }}
|
||||
{{ end }}
|
||||
<p>{{ summary . }}</p>
|
||||
<a href="{{ .Path }}">View</a>
|
||||
<p>{{ summary .Data }}</p>
|
||||
<a href="{{ .Data.Path }}">View</a>
|
||||
</article>
|
||||
<hr>
|
||||
{{ end }}
|
|
@ -1,22 +1,21 @@
|
|||
{{ define "title" }}
|
||||
<title>{{ with p . "title" }}{{ . }} - {{end}}{{ (blog .Blog).Title }}</title>
|
||||
<title>{{ with p .Data "title" }}{{ . }} - {{end}}{{ .Blog.Title }}</title>
|
||||
{{ end }}
|
||||
|
||||
{{ define "main" }}
|
||||
<main class=h-entry>
|
||||
<article>
|
||||
{{ with title . }}<h1 class=p-name>{{ . }}</h1>{{ end }}
|
||||
{{ with .Published }}
|
||||
{{ with title .Data }}<h1 class=p-name>{{ . }}</h1>{{ end }}
|
||||
{{ with .Data.Published }}
|
||||
<p>{{ string "publishedon" }} {{ dateformat . "02. Jan 2006" }}</p>{{ end }}
|
||||
{{ with .Updated }}
|
||||
{{ with .Data.Updated }}
|
||||
<p>{{ string "updatedon" }} {{ dateformat . "02. Jan 2006" }}</p>{{ end }}
|
||||
{{ with .Content }}
|
||||
{{ with .Data.Content }}
|
||||
<div class=e-content>{{ md . }}</div>
|
||||
{{ end }}
|
||||
</article>
|
||||
{{ $taxonomies := (blog .Blog).Taxonomies }}
|
||||
{{ $post := . }}
|
||||
{{ range $i, $tax := $taxonomies }}
|
||||
{{ $post := .Data }}
|
||||
{{ range $i, $tax := .Blog.Taxonomies }}
|
||||
{{ $tvs := ps $post $tax.Name }}
|
||||
{{ if gt (len $tvs) 0 }}
|
||||
<p>In <b>{{ $tax.Title }}</b>:
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
{{ define "redirect" }}
|
||||
<!doctype html>
|
||||
<html lang={{ (blog .Blog).Lang }}>
|
||||
<html lang={{ .Blog.Lang }}>
|
||||
<meta charset="utf-8"/>
|
||||
<meta name="robots" content="noindex">
|
||||
<title>{{ .Permalink }}</title>
|
||||
<link rel="canonical" href="{{ .Permalink }}"/>
|
||||
<meta http-equiv="refresh" content="0; url={{ .Permalink }}"/>
|
||||
<title>{{ .Data }}</title>
|
||||
<link rel="canonical" href="{{ .Data }}"/>
|
||||
<meta http-equiv="refresh" content="0; url={{ .Data }}"/>
|
||||
{{ end }}
|
|
@ -1,9 +1,9 @@
|
|||
{{ define "summary" }}
|
||||
<article>
|
||||
{{ with p . "title" }}<h2>{{ . }}</h2>{{ end }}
|
||||
{{ with .Published }}<p>{{ dateformat . "02. Jan 2006" }}</p>{{ end }}
|
||||
<p>{{ summary . }}</p>
|
||||
<a href="{{ .Path }}">View</a>
|
||||
{{ with p .Data "title" }}<h2>{{ . }}</h2>{{ end }}
|
||||
{{ with .Data.Published }}<p>{{ dateformat . "02. Jan 2006" }}</p>{{ end }}
|
||||
<p>{{ summary .Data }}</p>
|
||||
<a href="{{ .Data.Path }}">View</a>
|
||||
</article>
|
||||
<hr>
|
||||
{{ end }}
|
|
@ -1,14 +1,14 @@
|
|||
{{ define "title" }}
|
||||
<title>{{ .Taxonomy.Title }} - {{ (blog .Blog).Title }}</title>
|
||||
<title>{{ .Data.Taxonomy.Title }} - {{ .Blog.Title }}</title>
|
||||
{{ end }}
|
||||
|
||||
{{ define "main" }}
|
||||
<main>
|
||||
{{ with .Taxonomy.Title }}<h1>{{ . }}</h1>{{ end }}
|
||||
{{ with .Taxonomy.Description }}{{ md . }}{{ end }}
|
||||
{{ with .Data.Taxonomy.Title }}<h1>{{ . }}</h1>{{ end }}
|
||||
{{ with .Data.Taxonomy.Description }}{{ md . }}{{ end }}
|
||||
<ul>
|
||||
{{ $taxonomy := .Taxonomy.Name }}
|
||||
{{ range $i, $value := (sort .TaxonomyValues) }}
|
||||
{{ $taxonomy := .Data.Taxonomy.Name }}
|
||||
{{ range $i, $value := (sort .Data.TaxonomyValues) }}
|
||||
<li><a href="/{{ $taxonomy }}/{{ urlize . }}">{{ . }}</a></li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
|
|
Loading…
Reference in New Issue