published on 30.08.2020

The question is clear. We need to pass variables or arguments in Hugo’s partials. We usually use only the dot when calling partials. {{ partial "foo.html" . }}. The dot means the current page’s context. All it’s variables like .Title, .Permalink, .Content and all others will pass on with that dot.

Sometimes, we need to send more. For example, I’ve got a partial named generate_button.html and pass the href, name, and color variables. None of them are related to page content. To pass these arguments to the “partial” we need to use a dictionary instead of passing the dot.

{{ partial "helpers/generate_button.html" (dict "href" "/tags" "name" "Display by Tags" "theme" "paper-theme")}}

In this case, I can’t access any of the variables of the page inside the partial. .Title won’t work. Because we didn’t pass the dot. So, we need to include the dot in our dictionary.

{{ partial "your_crazy_good_partial.html" (dict "context" . "arg1" "val1" "arg2" "val2") }}

Now, in order to access page variables from “partial”, we need to prefix the dot’s name which is “context”. .context.Title, .context.Date, etc…

There is one more way, it’s called scratch. I mentioned it before in Loops in Hugo. Basically, with scratch, we can add variables into the page’s content then pass the dot alone instead of passing a dictionary. By using Scratch, you may get more clean and elegant code.

{{ .Scratch.Set "arg1" "val1" }}
{{ partial "your_crazy_good_partial.html" . }}

With this method, we can access all the standard page variables as we normally do. We don’t need to call .context.Title to get .Title. This is a solid advantage over the dictionary method. To access our special variable, val1, we need to use {{ .Scratch.Get "arg1" }}.

Here is an example of how I used .Scratch. I wanted to use a different color theme depending on the post-card’s index in the list. So, while looping through posts I wanted to send index. This is a very common use case, “how to get index in a range?". Instead of, using the dictionary method, it can be done using “scratchpad”.

{{ .Scratch.Set "index" 0 }}
{{ range $pages }}
  {{ $.Scratch.Set "index" (add ($.Scratch.Get "index") 1) }}
  {{ .Scratch.Set "index" ($.Scratch.Get "index") }}
  {{ partial "cards/post_card_classic.html" . }}
{{ end }}

4th line here sets the scratch variable of the page in loops' iteration. While $.Scratch indicates the top-level variable which means the main page outside of the loop. Then I can access the index variable in post_card_classic partial by calling {{.Scratch.Get "index"}}.

You are reading the 12nd of 19 episodes in Hugo.
Published on 30.08.2020 by Mert Bakır with commit db8d56a.
#hugo #static-site
Next episode:
Loops in Hugo
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 …

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 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 ‘’ and ‘’. 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 …