Recipes on More Contentful
© 2024
Hugo
2020-05-12T17:25:55+08:00
https://foo-dogsquared.github.io/hugo-theme-more-contentful/https://foo-dogsquared.github.io/hugo-theme-more-contentful/icon.png
https://foo-dogsquared.github.io/hugo-theme-more-contentful/recipes/configurable-list-of-contacts/
Configurable list of contacts
2020-10-20T20:38:24+08:00
2020-10-20T20:38:24+08:00<div class="paragraph">
<p>Most themes offer quick social media links with site configuration.
However, it is only limited to popular media sites such as Facebook, Twitter, Instagram, GitHub, etc.</p>
</div>
<div class="paragraph">
<p>To get around this, we’ll make use of <a href="https://gohugo.io/templates/data-templates/">data templates</a>.
Let’s create a quick game plan how does it work.</p>
</div>
<div class="paragraph">
<p>The data is a top-level dictionary/object with each key contains an object with the following fields.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>url</code> is the… contact link itself and it is required to have it.</p>
</li>
<li>
<p><code>name</code> is the text to appear in the output.
Also required to have.</p>
</li>
<li>
<p><code>weight</code> is an integer similar to how Hugo sorts the pages with the lower weight having high precedence;
if this key is absent, it will be interpreted as 0.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>And here’s the data example in TOML which is placed in <code>data/contact.toml</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-toml" data-lang="toml">[github]
name = "GitHub"
url = "https://github.com/foo-dogsquared"
[gitlab]
name = "Gitlab"
url = "https://gitlab.com/foo-dogsquared"
[keybase]
name = "Keybase"
url = "https://keybase.io/foo_dogsquared"
weight = -1
[twitter]
name = "Twitter"
url = "https://twitter.com/foo_dogsquared"</code></pre>
</div>
</div>
<div class="paragraph">
<p>I want my Keybase profile to appear first than anything else for whatever reason so the <code>weight</code> key is set to -1.</p>
</div>
<div class="paragraph">
<p>With this data, we can then create a template out of it.
I’ve put the following template in a partial named <code>contacts</code> (i.e., <code>layouts/partials/contacts</code>).</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-go" data-lang="go"><address>
{{- range (sort $.Site.Data.contacts "weight" "asc") -}}
| <a rel="me" href="{{ .url }}">{{- .name -}}</a> |
{{- end -}}
</address></code></pre>
</div>
</div>
<div class="paragraph">
<p>A suggestion (and an exercise) for extending this is to create image links.
Maybe add another key named <code>image</code> that accepts either URL.
The <code>name</code> would now be the image alternative text.</p>
</div>
https://foo-dogsquared.github.io/hugo-theme-more-contentful/recipes/creating-an-archive-page/
Creating an archive page
2020-10-20T20:36:55+08:00
2020-10-20T20:36:55+08:00<div class="paragraph">
<p>This will add an archive page similar to archive pages <a href="https://davidtranscend.com/archives/">like</a> <a href="https://lukesmith.xyz/blogindex.html">these</a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-go" data-lang="go">{{- define "main" -}}
<h1>{{ .Title }}</h1>
{{ .Content }}
<hr>
{{- /* Creating a section that lists out regular pages by year */ -}}
{{ range $.Site.RegularPages.GroupByPublishDate "2006" }}
{{- /* Skip regular pages with an invalid creation date string. */ -}}
{{- /* This is convenient if we want to exclude certain posts to be listed by giving no value to `date` in the frontmatter. */ -}}
{{- /* We will also exclude hidden pages. */ -}}
{{ if ne .Key "0001" }}
<section data-year="{{ .Key }}">
<h2 id="{{ .Key }}">{{ .Key }}</h2>
<ul>
{{- range where .Pages "Params.hidden" "!=" true -}}
<li>
<date>{{ .Date.Format "2006-01-02" }}</date> -
<a aria-label="{{ .Title }}" href="{{ .Permalink }}">{{ .Title }}</a>
</li>
{{- end -}}
</ul>
</section>
{{- end }}
{{ end }}
{{- end -}}</code></pre>
</div>
</div>
<div class="paragraph">
<p>We will simply add this as a layout in our customized theme.
Let’s call it <code>archives</code> so we have to add a file in <code>layouts/_default/archives.html</code> then set a page of our project with the <code>layout</code> key in the frontmatter.</p>
</div>
<div class="paragraph">
<p>We want the archives page to be accessed at <code>$.Site.BaseURL/archives</code> so we’ll simply create <code>archives.adoc</code> (<a href="https://gohugo.io/content-management/formats/#list-of-content-formats">any valid content files with certain file extensions can do</a>, I’m using <a href="https://asciidoctor.org/">Asciidoctor</a>) with the following example content.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-asciidoctor" data-lang="asciidoctor">---
title: "Archives"
layout: "archive"
---
= Archives
This is the archives of the century.</code></pre>
</div>
</div>