edited on 17.01.2022
In Hugo and generally other static website generators, we are using data files to store data. These files can be in YAML, TOML, or JSON formats. You can always read more about data template in the original documentation. Yet, I want to give some examples from my blog. First of all, converting dates into your language is a good example. Besides that, I’ve 3 data files. All three of them help me to customize sections. You can keep any info you want in these files. For example, you may have different authors on your site and you may want to store author information or you can be a small store and you may keep product information, book information, discography, any metadata you can think of…
Example 1
I keep my blog posts in different sections. You can think of them as folders. I wanted to set different banner images for each section. To do that, I’ve created a partial get_banner_image.html
and set banner info in a data file /data/banner.yaml
.
default: /link/to/default/image
hugo: /link/to/a/local/image/under/static/folder
jekyll: https://unsplash.com/...
linear programming: ...
{{ $bannerImage := "" }}
{{ if .Params.image }}
{{ $bannerImage = .Params.image }}
{{ else if and (ne .Section "") (index $.Site.Data.banner .Section) }}
{{ $bannerImage = index $.Site.Data.banner .Section }}
{{ else }}
{{ $bannerImage = index $.Site.Data.banner "default" }}
{{ end }}
{{ return $bannerImage }}
Then, I use this partial in another partial banner.html
, <img src="{{ partial "get_data/get_banner_image.html" . }}" alt="header-picture">
. So, each page can get it’s own banner image if it’s set in the page’s front-matter. If not, it’ll get the default image from the YAML file.
Example 2
The next example is pretty much the same as the “banner image” example. I’ve created themes in sass with different color schemes and section_themes.yaml
data file to map section names to a specific theme. Then, I created a partial template called get_theme.html
and used it to set theme class by section name.
Values on the right-hand side are corresponding CSS classes while the left side is section (folder) names.
default: paper-theme
hugo": paper-theme
statistics: almost-white-theme
operations-research: eflatun-theme
get_theme.html:
{{ $theme := index $.Site.Data.section_themes "default" }}
{{ if and (ne .Section "") (index $.Site.Data.section_themes .Section) }}
{{ $theme = index $.Site.Data.section_themes .Section }}
{{ end }}
{{ if .Params.theme }}
{{ $theme = .Params.theme }}
{{ end }}
{{ return $theme }}
Related html line:
<div class="container {{ partial "get_data/get_theme.html" . }}">
Example 3
Another good example is a Hugo Theme called Resume A4 I’ve created. The theme highly utilizes data files, in fact it’s nothing but yaml files and a little css.
Lastly, you should be aware of that front-matters in markdown files are metadata. Even though these are separate files, the logic is the same. Config file in your hugo project also has the same logic. You can define variables in the config file and re-use them in partial files. So that you can change the variable later. Hard-coded elements will be harder to maintain when you want to change something.