Skip to content
Browse files

Adding archive and posts on homepage

  • Loading branch information...
1 parent 56f5528 commit 0bee1fc54c18c56fce449ba6b86e4ef96b1fb23b @matt-west committed
Showing with 66 additions and 9 deletions.
  1. +26 −6 main.go
  2. +6 −0 posts/another-post.html
  3. +1 −1 posts/hello-world.html
  4. +6 −0 posts/my-second-post.html
  5. +20 −0 posts/posts.json
  6. +7 −2 templates.html
View
32 main.go
@@ -17,22 +17,25 @@ type Page struct {
type Post struct {
Title string
Slug string
- Date string
+ Date string
Keywords string
Description string
- Tags []string
+ Tags []string
}
const assetPath = len("/")
const pagePath = len("/page/")
const postPath = len("/")
+const maxPosts = 2 // Number posts to display on homepage
+
// Pages
var pages = make(map[string]*Page)
var pageTemplates = make(map[string]*template.Template)
// Posts
var posts = make(map[string]*Post)
+var postsJSON []Post // Need this so that there is an ordered list of posts
var postTemplates = make(map[string]*template.Template)
// Templates
@@ -54,10 +57,8 @@ func init() {
pages[pagesJSON[i].Slug] = &pagesJSON[i]
}
-
// Parse Posts JSON Dict
postsRaw, _ := ioutil.ReadFile("posts/posts.json")
- var postsJSON []Post
err = json.Unmarshal(postsRaw, &postsJSON)
if err != nil {
panic("Could not parse Posts JSON!")
@@ -68,7 +69,6 @@ func init() {
posts[postsJSON[i].Slug] = &postsJSON[i]
}
-
// Parse and Cache Page Templates
for _, tmpl := range pages {
t := template.Must(template.ParseFile("./pages/" + tmpl.Slug + ".html"))
@@ -164,13 +164,32 @@ func assetHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[assetPath:])
}
+func archiveHandler(w http.ResponseWriter, r *http.Request) {
+ p := Page{"archive", "Archive", "", ""}
+
+ // Header
+ layoutTemplates.Execute(w, "Header", p)
+
+ // Archives
+ layoutTemplates.Execute(w, "Archive", postsJSON)
+
+ // Footer
+ layoutTemplates.Execute(w, "Footer", p)
+}
+
func indexHandler(w http.ResponseWriter, r *http.Request) {
p := pages["index"]
// Header
layoutTemplates.Execute(w, "Header", p)
- // TODO Loop Each Post
+ // Show Recent Posts
+ for i, tmpl := range postsJSON {
+ if i >= maxPosts {
+ break
+ }
+ postTemplates[tmpl.Slug].Execute(w, nil)
+ }
// Footer
layoutTemplates.Execute(w, "Footer", p)
@@ -178,6 +197,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
// Starts Server and Routes Requests
func main() {
+ http.HandleFunc("/archive", archiveHandler)
http.HandleFunc("/page/", pageHandler)
http.HandleFunc("/assets/", assetHandler)
http.HandleFunc("/", postHandler)
View
6 posts/another-post.html
@@ -0,0 +1,6 @@
+<article>
+ <h1>Another Post</h1>
+ <p>
+ Testing more stuff.
+ </p>
+</article>
View
2 posts/hello-world.html
@@ -1,6 +1,6 @@
<article>
<h1>Hello World</h1>
<p>
- This is just a test article
+ This is just a test article!
</p>
</article>
View
6 posts/my-second-post.html
@@ -0,0 +1,6 @@
+<article>
+ <h1>My Second Post</h1>
+ <p>
+ Testing stuff.
+ </p>
+</article>
View
20 posts/posts.json
@@ -1,5 +1,25 @@
[
{
+ "Title": "Another Post",
+ "Slug": "another-post",
+ "Date": "25 January 2012",
+ "Keywords": "Some keywords",
+ "Description": "A short descriptions",
+ "Tags": [
+ "Business"
+ ]
+ },
+ {
+ "Title": "My Second Post",
+ "Slug": "my-second-post",
+ "Date": "25 January 2012",
+ "Keywords": "Some keywords",
+ "Description": "A short descriptions",
+ "Tags": [
+ "Business"
+ ]
+ },
+ {
"Title": "Hello World",
"Slug": "hello-world",
"Date": "25 January 2012",
View
9 templates.html
@@ -22,15 +22,20 @@
<nav>
<ul class="clearfix">
<li><a href="/" title="Home">Home</a></li>
- <li><a href="/archives" title="Archives">Archives</a></li>
+ <li><a href="/archive" title="Archives">Archives</a></li>
<li><a href="/page/about" title="About">About</a></li>
<li><a href="/page/license" title="License">License</a></li>
</ul>
</nav>
{{end}}
-{{define "Archives"}}
+{{define "Archive"}}
<h1>Archives</h1>
+ <ul>
+ {{range .}}
+ <li><a href="{{.Slug}}" title="{{.Title}}">{{.Title}}</a></li>
+ {{end}}
+ </ul>
{{end}}
{{define "Footer"}}

0 comments on commit 0bee1fc

Please sign in to comment.
Something went wrong with that request. Please try again.