published on 23.08.2020


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/ 
# Page(/hugo/ 
# Page(/hugo/
# Page(/jekyll/ 
# ... 
# Page(/hugo/ 
# Page(/hugo/
{{ 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" }}
        {{}} &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 f5816db.
#hugo #static-site
Next episode:
Syntax Highlighting in Hugo
published on 30.08.2020

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 a 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 17.04.2022
edited on 02.05.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 …

published on 19.02.2022

Önceki yazıda LaTeX kullanmanın avantajlarından ve zorluklarından bahsetmiştim, MS Word gibi WYSIWYG programlar ile karşılaştırıp tavsiyelerimi yazmıştım. Bu yazıda Pandoc yazılımı üzerine konuşacağız. Pandoc’un da yardımıyla LaTeX’in avantajlarını alıp zorluklarını biraz geri de …

published on 12.02.2022

LaTeX bir çeşit döküman hazırlama sistemidir, ancak çoğumuzun alıştığı arayüzlerin aksine bu döküman içerisinde kod yazar gibi bazı komutlar kullanırız. Daha sonra derleyici (compiler) bu dökümanı bizim tanımladığınız kurallara göre PDF’e dönüştürür. […] 1978 yılında, Donald Knuth TeX …

published on 02.01.2022

This post is for fellow students who is studying Operations Research with CPLEX. If you don’t know already, IBM offers free academic license for IBM ILOG CPLEX Optimization Studio. […] First, you have to register IBM Academic Initiative with your academic instituion issued email. (e.g. …