× Search About Posts Code Music Links
Blank Try
experiment
lots
learn
more

Sidebar Code

Updated on
Last site update: 8 May 2024

Here is the code from the sidebar on the right as it was, more or less, from the blank theme:

1<ul>
2    {{ range first 15 (where .Site.RegularPages "Type" "in" .Site.Params.mainSections) }}
3        <li><a href="{{ .RelPermalink }}">{{ .Title }}<small> ({{ .Date.Format "2 Jan 06" }})</small></a> </li>
4    {{ end }}
5</ul>

The use of in is interesting. According to Hugo docs in:

Checks if an element is in an array or slice–or a substring in a string—and returns a boolean.

It goes on to say:

The elements supported are strings, integers and floats, although only float64 will match as expected.

In addition, in can also check if a substring exists in a string.

The examples used are:

1{{ if in .Params.tags "Git" }}Follow me on GitHub!{{ end }}

mainSections are listed in the config file:

1[params]
2  mainSections = ["posts", "code"]

Latest code

The latest sidebar code does two more things.

First it changes the content of the sidebar depending on whether it’s a not a section page (eg home page, links, about or tags)—where it lists all recent posts from all mainSections (those listed in the config file)—or another page, where it lists the latest posts from that section.

Secondly it creates active links for the current page which can be targetted in CSS with an attribute selector: a[href="#"]****.

 1<ul>
 2  {{ if eq .Section "" }}
 3    {{ range first 15 (where .Site.RegularPages "Type" "in" .Site.Params.mainSections) }}
 4    <li><a href="{{ .RelPermalink }}">{{ .Title }}<small> ({{ .Date.Format "2 Jan 06" }})</small></a> </li>
 5    {{ end }}
 6
 7  {{ else }}
 8    {{ $currentPage := . }}
 9    {{ range first 15 (where .Site.RegularPages "Type" "in" .Section) }}
10      <li>
11            
12        {{ if eq $currentPage . }}
13          <a href="#">{{ .Title }}<small> ({{ .Date.Format "2 Jan 06" }})</small></a>
14        {{ else }}
15          <a href="{{ .RelPermalink }}">{{ .Title }}<small> ({{ .Date.Format "2 Jan 06" }})</small></a>
16        {{ end }}
17                
18      </li>
19    {{ end }}
20  {{ end }}
21</ul>