Preface
This article is intended for beginner.
Goal: Building content, and produce list for section.
1: Section
I have been looking for a good content for these tutorial. And believe me it is as hard as giving a good variable naming. I look for simple content, such as churchill quotes, also navier-stokes explanation, and finally end up with quotes song lyric.
So yes, I decide to create a section named quotes.
Archetypes
Since every quotes having similar frontmatter, we can make a frontmatter template, so we do not have to type each time we make a new document.
Consider this archetypes for quotes.
-
- themes/tutor-04/archetypes/quotes.md
- gitlab.com/…/archetypes/quotes.md
+++
type = "post"
title = "{{ replace .TranslationBaseName "-" " " | title }}"
date = {{ .Date }}
categories = []
tags = []
slug = ""
author = "epsi"
draft = true
+++
Permalink
For this quotes section, we can also set the permalink behaviour.
-
- config.toml
- gitlab.com/…/config.toml
[permalinks]
posts = ":section/:year/:month/:day/:slug"
quotes = ":section/:year/:month/:day/:slug"
2: Populate the Content
Now you can easily make ten content with archetypes.
Example Content
$ hugo new quotes/bruce-springsteen-the-river
/media/Works/sites/tutor-hugo/content/quotes/bruce-springsteen-the-river created
The result will be
-
- content/quotes/bruce-springsteen-the-river.md
- gitlab.com/…/…the-river.md
+++
type = "post"
title = "Bruce Springsteen the River"
date = 2018-09-15T07:35:05+07:00
categories = ["lyric"]
tags = []
slug = ""
author = "epsi"
draft = true
+++
Edit the frontmatter and add content, so that we have something similar as below:
+++
type = "post"
title = "Bruce Springsteen - The River"
date = 2014-03-15T07:35:05+07:00
categories = ["lyric"]
tags = [springsteen, 80s]
slug = "bruce-springsteen-the-river"
author = "epsi"
+++
Is a dream a lie
if it don't come true?
Or is it something worse?
And The Rest
Consider do the rest for these nine lyrics:
$ for i in quotes/{\
"bruce-springsteen-one-step-up",\
"joni-mitchell-both-sides-now",\
"john-mayer-slow-dancing-in-a-burning-room",\
"julian-baker-something",\
"sarah-mclachlan-angel",\
"avenged-sevenfold-so-far-away",\
"michael-jackson-gone-too-soon",\
"mothers-no-crying-in-baseball",\
"blink-182-adams-song"}.md;\
do hugo new "$i"; done
/media/Works/sites/tutor-hugo/content/quotes/bruce-springsteen-one-step-up.md created
/media/Works/sites/tutor-hugo/content/quotes/joni-mitchell-both-sides-now.md created
/media/Works/sites/tutor-hugo/content/quotes/john-mayer-slow-dancing-in-a-burning-room.md created
/media/Works/sites/tutor-hugo/content/quotes/julian-baker-something.md created
/media/Works/sites/tutor-hugo/content/quotes/sarah-mclachlan-angel.md created
/media/Works/sites/tutor-hugo/content/quotes/avenged-sevenfold-so-far-away.md created
/media/Works/sites/tutor-hugo/content/quotes/michael-jackson-gone-too-soon.md created
/media/Works/sites/tutor-hugo/content/quotes/mothers-no-crying-in-baseball.md created
/media/Works/sites/tutor-hugo/content/quotes/blink-182-adams-song.md created
Also edit the frontmatter and add content, for the rest of the nine lyrics.
Result
Now we can see the list:
Now we need to design a more informative template layout for this content list.
3: Listing
Consider change this artefact:
-
- themes/tutor-04/layouts/_default/list.html
- gitlab.com/…/layouts/_default/list.html
Instead of all kind of pages, we filter it to show only post type.
{{ define "main" }}
<main role="main"
class="container-fluid m-3 m-sm-0 p-3
bg-light rounded border border-dark shadow-hover">
<header>
<h4>{{ .Section }}</h4>
</header>
{{ .Content }}
{{ $posts := where .Data.Pages "Type" "post" }}
<section id="archive">
{{ range ($posts.GroupByDate "2006") }}
{{ partial "summary-by-year.html" . }}
{{ end }}
</section>
</main>
{{ end }}
Listing By Year
Instead of simple loop, we can group the article by year. The detail of the partial layout can bee seen here.
- themes/tutor-04/layouts/partials/summary-by-year.html : gitlab.com/…/partials/summary-by-year.html
<div class ="anchor-target archive-year"
id="{{ .Key }}">{{ .Key }}</div>
{{ range .Pages }}
<div class="d-flex flex-row-reverse archive-list">
<div class="text-right "><time>
{{ .Date.Format "02 Jan" }} </time></div>
<div class="mr-auto"><a href="{{ .URL | absURL }}">
{{ .Title }}
</a></div>
</div>
{{ end }}
Add custom scss, so the list can have better looks.
-
- themes/tutor-04/sass/css/_decoration.scss
- gitlab.com/…/sass/_decoration.scss.
// -- -- -- -- --
// _list.scss
.archive-list {
position: relative;
}
.archive-list a:before {
position: absolute;
left: -15px;
content: "\00BB";
color: $dark;
}
.archive-list:hover {
background-color: $yellow;
}
.
And do not forget to put the newly add custom scss to the main one.
-
- themes/tutor-04/sass/css/main.scss
- gitlab.com/…/sass/main.scss.
@import
...
// custom
"layout",
"decoration",
"list"
;
Listing By Month
Of course, you can make, a more sophisticated listing, by changing this artefact:
-
- themes/tutor-04/layouts/_default/list.html
- gitlab.com/…/layouts/_default/list.html
{{ define "main" }}
<main role="main"
...
<section id="archive">
{{ range ($posts.GroupByDate "2006") }}
{{ partial "summary-by-month.html" . }}
{{ end }}
</section>
</main>
{{ end }}
We use multiple loop stage. Outer: Group the article by year. And inner: Group Article by month.
- themes/tutor-04/layouts/partials/summary-by-month.html : gitlab.com/…/partials/summary-by-month.html
{{ $year := .Key }}
<div class ="anchor-target archive-year"
id="{{ .Key }}">{{ $year }}</div>
{{ range (.Pages.GroupByDate "January") }}
{{ $month := .Key }}
<div class="px-3">
<span class ="anchor-target archive-month"
id="{{ $year }}-{{ $month }}">
{{ $month }} - {{ $year }}</span>
<div class="px-3">
{{ range .Pages }}
<div class="d-flex flex-row-reverse archive-list">
<div class="text-right text-nowrap"><time>
{{ .Date.Format "02 Jan" }} </time></div>
<div class="mr-auto"><a href="{{ .URL | absURL }}">
{{ .Title }}
</a></div>
</div>
{{ end }}
</div>
</div>
{{ end }}
What is Next ?
There are, some interesting topic about Taxonomy in Hugo. Consider continue reading [ Hugo - Taxonomy ].
Thank you for reading.