published on 23.08.2020

Introduction

In Hugo, to loop through list of items we use Range function which comes from go language. range, iterates over a map, slice or array.

{{ range .Site.RegularPages }}
    {{ .Title }}
{{ end }}

Also, notice inside range the scope is different. .Title refers to the page in current iteration. If we need the top level variable we should the dollar sign $.Title. Scope is another topic and here is a very useful blog-post already written.

C Style For Loop

Loops in Hugo is a little different. We need small tricks to get a classic C style for loop. range is more like a “foreach” function than a “for”. Yet, this doesn’t mean we can’t use iteration number.

for (int i = 0, i < upperBound, i ++)
{
    //do something;
}

Key Value Pairs

So, how can we get the iteration number (or index)? Let’s start with a simple example using context (the dot) and seq function.

{{ range $num := (seq 5) }}
  {{ . }}
{{ end }}

# 1 2 3 4 5

We can use key-value pairs to generate the iteration number. In the example below $index is our key.

{{ range $index, $num := (seq 5) }}
  {{ $index }}
{{ end }}

# 0 1 2 3 4

$index is not a keyword here, just a variable we named, we could’ve call it $foo. Also, notice that it’s zero based.

We need to define the value too. See the difference in following examples:

{{ range $index, $.Site.RegularPages }}
  {{ $index }} <br>
{{ end }}

# Page(/hugo/convert-dates-into-your-language.md) 
# Page(/hugo/last-modified-date-in-hugo.md) 
# Page(/hugo/tag-cloud-in-hugo.md)
# Page(/jekyll/rss-feed-in-jekyll.md) 
# ... 
# Page(/hugo/loops-in-hugo.md) 
# Page(/hugo/math-typesetting-in-hugo.md)
{{ range $index, $val := $.Site.RegularPages }}
  {{ $index }}
{{ end }}

# 0 1 2 3 ... 49 50

Scracth Function

Alternatively, we can use the Scracth function. Most of the time, key, value pairs above will suffice. Yet, $index always starts from 0 and increments by 1 at each iteration. With, scracth we don’t have to start from 0 and increment by 1. It’s up to us.

{{ $.Scratch.Set "index" 0 }}
{{ range $pages }}
  {{ $.Scratch.Set "index" (add ($.Scratch.Get "index") 1) }}
  //do something
{{ end }}

Main purpose of scratch is handling scoping issues. It basically allows us to set variables into the context of the page. Then we can manipulate, change or get these scracth variables from other pages, it’s most helpful when creating shortcodes and partials. Here is a good article about scracth.

Nested Loops in Hugo

Nested loops in Hugo is easy but you should know that range has it’s own scope and you need to use the dollar sign $ to get variable from the root page. Here is an example I’ve used before.

{{ $sections := where $.Site.Pages ".Kind" "section" }}

{{ range $sections }}
    <h3><a href="{{ .Permalink }}">{{ .Title }}</a></h3>
    {{ $sectionName := .Section | singularize }}
    {{ $pages := where $.Site.RegularPages ".Section" $sectionName }}

    {{ range $pages.ByParam "date" }}
        {{.Params.date}} &middot; {{.Title}}
    {{ end }}
{{ end }}
You are reading the 13rd of 19 episodes in Hugo.
Published on 23.08.2020 by Mert Bakır with commit db8d56a.
hugo
#hugo #static-site
Next episode:
Syntax Highlighting in Hugo
published on 30.08.2020
edited on 15.07.2022

Mainly there are two ways of syntax highlighting. One is server-side and the other is client-side. Names are already self-explanatory, client-side highlighting occurs on the user’s browser via javascript. Highlight.js is one of the popular libraries which covers crazy amount of languages. On …

published on 29.08.2020

My experience with Hugo has been pretty good so far. It’s fast and flexible. Ever since I started using Hugo, I’ve been improving my website with small tweaks. In this post, I am going to share some tricks and workarounds I’ve found online while working with Hugo. Besides that, …

published on 18.09.2020

First of all, if you don’t know about data files, you may want to start reading from data files in hugo. This post is about a solution for a very particular problem. How can we use “group by” for the data from data files? Let me clarify with an example. I was creating a single page …

published on 10.07.2022

Previously, I’ve published a blog post about deploying static content on heroku with basic authentication. The main purpose was to get basic auth for a freely hosted static website. In that post, we hosted the source code on GitLab and configured a CI/CD pipeline to render the static content …

published on 28.05.2022

Each git commit has a field called Author which consists ‘user.name’ and ‘user.email’. We usually set these variables once, after installing git, with git config --global so that each repo gets the variables from the global definition. We can also set them locally for a …

published on 25.05.2022

In this post, I’ll first walk through hosting static content with basic authentication. Then, we’ll look into deploying to Heroku using GitLab Pipelines, more specifically deploying a certain sub-directory within the project instead of pushing the whole project. Also, I’ll share …

published on 17.04.2022
edited on 15.07.2022

Önceki bölümde, markdown formatını LaTeX formatına dönüştürmek için kullanılan Pandoc yazılımından bahsetmiştik. Şimdi konuyu bir adım daha ileri taşıyıp ve bookdown’a geçiyoruz. Bookdown; Rmarkdown kullanarak teknik dökümanlar, kitaplar yazabilmemizi sağlayan, Yihui Xie tarafından yazılmış …

published on 10.04.2022

I’ve been using WSL-2 on Windows for over a year. It’s very useful because some Python packages are just a headache to install on Windows. Also, docker. It’s just better on Linux. Yet, WSL-2 can also be problematic. I remember trying a dual-boot setup when things just went way too …

published on 03.03.2022

In this post, I’ll share how to install geopandas and some other gis related packages on Windows. If you are on Mac or Linux you can probably just pip install those without any issue. I usually had to do a google search every time I wanted to install these packages on Windows environment. Of …