refactor: nest templates, extract base template
This commit is contained in:
parent
82f1c4bbb2
commit
3a0d758ef7
5 changed files with 58 additions and 87 deletions
12
routes.go
12
routes.go
|
@ -26,8 +26,8 @@ func healthcheck(w http.ResponseWriter, r *http.Request) {
|
||||||
//
|
//
|
||||||
// Static content for the about page.
|
// Static content for the about page.
|
||||||
func about(w http.ResponseWriter, r *http.Request) {
|
func about(w http.ResponseWriter, r *http.Request) {
|
||||||
tmpl, _ := template.New("about.html.tmpl").ParseFiles("templates/about.html.tmpl")
|
tmpl, _ := template.New("about").ParseFiles("templates/about.html.tmpl", "templates/base.html.tmpl")
|
||||||
tmpl.Execute(w, nil)
|
tmpl.ExecuteTemplate(w, "base", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Feeds list
|
// Feeds list
|
||||||
|
@ -41,8 +41,8 @@ func listContent(w http.ResponseWriter, r *http.Request) {
|
||||||
links = append(links, formattedItems...)
|
links = append(links, formattedItems...)
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, _ := template.New("index.html.tmpl").ParseFiles("templates/index.html.tmpl")
|
tmpl, _ := template.New("base").ParseFiles("templates/index.html.tmpl", "templates/base.html.tmpl")
|
||||||
tmpl.Execute(w, links)
|
tmpl.ExecuteTemplate(w, "base", links)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Manage content
|
// Manage content
|
||||||
|
@ -74,6 +74,6 @@ func manageContent(w http.ResponseWriter, r *http.Request) {
|
||||||
Feeds map[string]string
|
Feeds map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, _ := template.New("manage.html.tmpl").ParseFiles("templates/manage.html.tmpl")
|
tmpl, _ := template.New("manage").ParseFiles("templates/manage.html.tmpl", "templates/base.html.tmpl")
|
||||||
tmpl.Execute(w, ManageTmplData{Feeds: allFeeds})
|
tmpl.ExecuteTemplate(w, "base", ManageTmplData{Feeds: allFeeds})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,5 @@
|
||||||
<html>
|
{{ define "content" }}
|
||||||
<head>
|
<main>
|
||||||
<title>☕ Morning coffee</title>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<link href="/static/main.css", rel="stylesheet">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<header>
|
|
||||||
<h1>☕ Morning coffee</h1>
|
|
||||||
<nav>
|
|
||||||
<ul>
|
|
||||||
<li><a href="/about">About</a></li>
|
|
||||||
<li><a href="/">Feeds</a></li>
|
|
||||||
<li><a href="/manage">Manage</a></li>
|
|
||||||
</ul>
|
|
||||||
</nav>
|
|
||||||
</header>
|
|
||||||
<main>
|
|
||||||
<em>Morning Coffee</em> is a minimalist static-page RSS & interesting links reader written in Go.
|
<em>Morning Coffee</em> is a minimalist static-page RSS & interesting links reader written in Go.
|
||||||
</main>
|
</main>
|
||||||
</body>
|
{{ end }}
|
||||||
</html>
|
|
||||||
|
|
22
templates/base.html.tmpl
Normal file
22
templates/base.html.tmpl
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
{{ define "base" }}
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>☕ Morning coffee</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
|
<link href="/static/main.css", rel="stylesheet">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>☕ Morning coffee</h1>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li><a href="/about">About</a></li>
|
||||||
|
<li><a href="/">Feeds</a></li>
|
||||||
|
<li><a href="/manage">Manage</a></li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
{{ template "content" . }}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
{{ end }}
|
|
@ -1,27 +1,10 @@
|
||||||
<html>
|
{{ define "content" }}
|
||||||
<head>
|
<ul id="items">
|
||||||
<title>☕ Morning coffee</title>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<link href="/static/main.css", rel="stylesheet">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<header>
|
|
||||||
<h1>☕ Morning coffee</h1>
|
|
||||||
<nav>
|
|
||||||
<ul>
|
|
||||||
<li><a href="/about">About</a></li>
|
|
||||||
<li><a href="/">Feeds</a></li>
|
|
||||||
<li><a href="/manage">Manage</a></li>
|
|
||||||
</ul>
|
|
||||||
</nav>
|
|
||||||
</header>
|
|
||||||
<ul id="items">
|
|
||||||
{{ range . }}
|
{{ range . }}
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ .Url }}">{{ .Title }}</a>
|
<a href="{{ .Url }}">{{ .Title }}</a>
|
||||||
<span>{{ .PublishedDate }}</span>
|
<span>{{ .PublishedDate }}</span>
|
||||||
</li>
|
</li>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</ul>
|
</ul>
|
||||||
</body>
|
{{ end }}
|
||||||
</html>
|
|
||||||
|
|
|
@ -1,21 +1,5 @@
|
||||||
<html>
|
{{ define "content" }}
|
||||||
<head>
|
<main>
|
||||||
<title>☕ Morning coffee</title>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<link href="/static/main.css", rel="stylesheet">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<header>
|
|
||||||
<h1>☕ Morning coffee</h1>
|
|
||||||
<nav>
|
|
||||||
<ul>
|
|
||||||
<li><a href="/about">About</a></li>
|
|
||||||
<li><a href="/">Feeds</a></li>
|
|
||||||
<li><a href="/manage">Manage</a></li>
|
|
||||||
</ul>
|
|
||||||
</nav>
|
|
||||||
</header>
|
|
||||||
<main>
|
|
||||||
<form action="/manage" method="post">
|
<form action="/manage" method="post">
|
||||||
<label for="feed">Url</label>
|
<label for="feed">Url</label>
|
||||||
<input name="url" type="url" />
|
<input name="url" type="url" />
|
||||||
|
@ -27,6 +11,5 @@
|
||||||
<li>{{ . }}</li>
|
<li>{{ . }}</li>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</ul>
|
</ul>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
{{ end }}
|
||||||
</html>
|
|
||||||
|
|
Loading…
Reference in a new issue