Creating Labarum (Part 2): Creating the individual posts

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.

<article class="h-entry">
<header>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
</header>

<section class="e-content post-body">
{{ .Content }}
</section>

It renders to the following:

Simple article header with title

Let’s go back to our index.html to make it link to our post and show it on the main page.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
{{ range ( .Paginate (where .Site.RegularPages "Type" "post")).Pages }}

<article>
	<header>
		<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
	</header>
	<section>
		{{ .Content }}
	</section>
	<footer>
		<p>
		Category: <a href="#">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.

Links between index and post

We can test this by adding more content in the post folder to see what it looks like.

Multiple posts on index

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.

Mental model of post 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.

<article class="h-entry">
<header>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
</header>

<section class="e-content post-body">
{{ .Content }}
</section>

<footer>
	<p>Category: <a href="#">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.

{{ if .Title }}
<header>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
</header>
{{ end }}

Then let’s make a post without a header to test out that this is working without an error in our logs.

Simple article without a header

Next, we can work on the article footer which I’ll make into a separate component in the next installment.

Categories: Labarum

Published:

Debug

Site Properties

		&hugolib.SiteInfo{Authors:page.AuthorList(nil), Social:hugolib.SiteSocial(nil), hugoInfo:hugo.Info{CommitHash:"1798bd3f", BuildDate:"2021-12-23T15:33:34Z", Environment:"production"}, title:"Mandaris", RSSLink:"https://mandarismoore.com/feed.xml", Author:map[string]interface {}{"avatar":"https://micro.blog/mandaris/avatar.jpg", "name":"Mandaris Moore", "username":"mandaris"}, LanguageCode:"en", Copyright:"", permalinks:map[string]string{}, LanguagePrefix:"", Languages:langs.Languages{(*langs.Language)(0xc00080dba0)}, BuildDrafts:false, canonifyURLs:false, relativeURLs:false, uglyURLs:(func(page.Page) bool)(0x163ea20), owner:(*hugolib.HugoSites)(0xc0000ed600), s:(*hugolib.Site)(0xc000592d80), language:(*langs.Language)(0xc00080dba0), defaultContentLanguageInSubdir:false, sectionPagesMenu:""} 

		

site.Params Properties

		maps.Params{"description":"Follow <a href=\"https://micro.blog/mandaris\">@mandaris on Micro.blog</a>.", "feeds":maps.Params{"bookmarks_json":"https://micro.blog/feeds/mandaris/bookmarks/08117195b525afe4a55f.json"}, "github_username":"mandaris", "include_conversation":true, "instagram_username":"mandaris", "itunes_author":"Mandaris", "itunes_category":"Society & Culture", "itunes_cover":"https://micro.blog/mandaris/podcast.png", "itunes_description":"A High-quality stranger. I've considered myself a digital gofer. Y'know, go for this and go for that. I'm using micro.blog now to par down my technical projects and be part of an outstanding community.  Apple Game Center: MrMandaris  Email: Contact@MandarisMoore.com", "itunes_email":"podcast@mandarismoore.com", "itunes_subcategory":"Personal Journals", "mainSections":[]string{"replies"}, "mainsections":[]string{"replies"}, "paginate_categories":true, "paginate_home":true, "paginate_replies":true, "plugins_css":[]interface {}{}, "plugins_html":[]interface {}{}, "plugins_js":[]interface {}{}, "site_id":"661", "theme_seconds":"1675089916", "twitter_username":"mandaris"}
		

Permalink

		"https://mandarismoore.com/2022/12/29/creating-labarum-part.html" 

		

Params

		map[categories:[Labarum] date:2022-12-29 19:09:58 -0800 -0800 draft:%!s(bool=false) guid:http://mandaris.micro.blog/2022/12/29/creating-labarum-part.html images:[https://mandarismoore.com/uploads/2022/0a0dc4df16.png https://mandarismoore.com/uploads/2022/a4b66784cd.png https://mandarismoore.com/uploads/2022/bf4479aaed.png https://mandarismoore.com/uploads/2022/978e2e8cd2.png https://mandarismoore.com/uploads/2022/9fa69be141.png] iscjklanguage:%!s(bool=false) lastmod:2022-12-29 19:09:58 -0800 -0800 layout:post microblog:%!s(bool=false) photos:<nil> photos_with_metadata:<nil> publishdate:2022-12-29 19:09:58 -0800 -0800 title:Creating Labarum (Part 2): Creating the individual posts type:post url:/2022/12/29/creating-labarum-part.html]
		

All variables scoped to the current context

		&hugolib.pageState{pageOutputs:[]*hugolib.pageOutput{(*hugolib.pageOutput)(0xc0026f46c0), (*hugolib.pageOutput)(0xc0026f47e0), (*hugolib.pageOutput)(0xc0026f4900), (*hugolib.pageOutput)(0xc0026f4a20), (*hugolib.pageOutput)(0xc0026f4b40), (*hugolib.pageOutput)(0xc0026f4c60), (*hugolib.pageOutput)(0xc0026f4d80), (*hugolib.pageOutput)(0xc0026f4ea0)}, pageOutput:(*hugolib.pageOutput)(0xc0026f46c0), pageCommon:(*hugolib.pageCommon)(0xc0018c0a00)}