I am quite sure there is a better way to do this. But this is how I did it.
You can see a demo of it up on http://parsiya.io/archive/. Is this what you are looking for?
Here’s how I did it.
Content types are determined by their directory. For example anything in content/post/
is of type post
.
There is another way to assign types and it is in front matter. This will override the directory structure. For example: type: thisisatype
in front matter.
Create a markdown file in content
. The name of the file is the same as the subdirectory that you want the page to appear on the website. For example in this I have named it archive.markdown
so it appears in http://parsiya.io/archive/. I will assign it a type. This can be anything. I have named it mycustomtype
.
---
title: "Blog Index"
type: mycustomtype
---
This is the text in the body of the markdown post.
Now create the file layouts/mycustomtype/single.html
. Directory name is the same as the type. Obviously the directory also needs to be created. This file will be the template for your archive page.
You can iterate through all posts by using the .Site.Pages
variable like this:
{{ range .Site.Pages }}
or iterate through certain content types. In my case, my normal pages are of type post
so I only go through posts like this:
{{ range (where .Site.Pages "Type" "post") }}
Here’s the source for my layouts/mycustomname/single.html
. You can see that I have also used the {{ .Content }}
variable before the range which contains the content of the archive.markdown
file. Inside the range you can access different variables for each page such as Title, Date, etc. You can also print tags and categories.
{{ partial "header.html" . }}
<div id="main">
<div id="content">
<div>
<article role="article">
<header>
<h1 class="entry-title">
{{ .Title }} <!-- title, in this case it will be "blog index" -->
</h1>
</header>
<div id="blog-archives" class="category">
{{ .Content }} <!-- content of the markdown file. note that inside the range .Content will point to each page's content -->
{{ range (where .Site.Pages "Type" "post") }}
<h2>
{{ .Date | dateFormat "2006"}} <!-- print publish year -->
</h2>
<article>
<h1>
<a href="{{ .Permalink }}" title="{{ .Title }}">{{ .Title }}</a>
</h1>
<time>
<span class="month">{{ .Date | dateFormat "Jan" }}</span> <!-- print publish month -->
<span class="day">{{ .Date | dateFormat "2" }}</span> <!-- print publish day -->
</time>
<!-- if you want pages summary you can print it here {{ .Summary }} -->
</article>
{{ end }}
</div>
</article>
</div>
{{ partial "sidebar.html" . }}
</div>
</div>
{{ partial "footer.html" . }}
EDIT: Added some comments to the template.
EDIT 2: You can also paginate similar to index.html
template.