For this part, we’re going to be setting up the basic page used for our post and make sure that it can be reached from the index page. We’re going to be switching between different files to show the iterative process that I went through in order to see results quickly.
We’re going to edit the single.html to show the content by using the {{ .Content }} tag from Hugo. We also add the {{ .Title }} to the header and link it using the {{ .Permalink }} tag to get the following.
{{range(.Paginate(where.Site.RegularPages"Type""post")).Pages}}
<article>
<header>
<h1><ahref="{{.Permalink}}">{{.Title}}</a></h1>
</header>
<section>
{{.Content}}
</section>
<footer>
<p>
Category: <ahref="#">Some Category</a><br/>
Tags: One Two Three
</p>
<p>Published: Some Date</p>
</footer>
</article>
{{end}}
The first line in the above code instructs hugo to iterate over all pages marked for the post type. On our local system, this isthe post directory and is in a database when using Micro.blog. The Permalink points to the individual post.
We can test this by adding more content in the post folder to see what it looks like.
But this highlights, we’re currently writing code in two places: the index.html and the single.html to render our content.
Note the forth line of the code which makes the assumption that we’re using a title for this post.
This is how I envision a post’s composition.
We’d check if a post has a title and from there show a header or not before displaying the content and ultimately the footer.
Abstracting the article from the page
Let’s create partials/article.html and we’ll put all the content that we placed in _default/single.html in it.
<articleclass="h-entry">
<header>
<h1><ahref="{{.Permalink}}">{{.Title}}</a></h1>
</header>
<sectionclass="e-content post-body">
{{.Content}}
</section>
<footer>
<p>Category: <ahref="#">Some Category</a><br/>
Tags: One Two Three</p>
<p>Published: Some Time</p>
</footer>
</article>
Then we replace the contents of default/single.html with the following.
{{define"main"}}{{partial"article.html".}}{{end}}
When we render the page and there shouldn’t be any errors at this point.
Let’s add the conditional for the header of the article.