Tags from site data files
Mostly, just need help with the go-templating language syntax, because go-html does not have a for
statement for iteration, and range
is just weird. Awk has one, so why not go?
But mostly, I could not find how concatenation of strings is supposed to work. Using “+” raised an error.
The problem
The hugo theme “Fresh” is used to generate a single page website, and utilizes the popular bulma sass library. The frontpage of the site is broken down into “sections”, and the content for each section is extracted from a yaml file located in the data directory. There is one huge drawback to using this theme, it does not generate taxonomy.
To fix this, I am creating the missing templates, which delightfully hugo does most of the legwork automatically, because it is so awesome like that. I just need to inform hugo where to extract the tags from and how to associate those tags back to .Site.BaseURL
using proper link structure to link to the section and not the main page.
Attempted resolution
Because not all files located in the data dir are being used to generate site data, the first step was to define what files should be used to pull data from in the .Site.Params
section of hugo.yaml
. I labeled this variable “frames”, because the individual sections of the frontpage are similar to frames of a movie, and if I used “sections” it would be confused with the hugo site.Sections
object.
If needed due to making it overly complex, I can just remove this and delete the additional files.
params:
#...
frames:
- Data1
- Data2
- Data3
- Data4
- ...
Next the layouts/_default/taxonomy.html
template was created using information provided in hugo docs and partials from the frontpage. Then variables were created to help flush out the remainder of the template, and provide direction for hugo as to where the information for taxonomy is supposed to come from. It is the latter where we encountered our problem.
Below are the variables created at the top of the template, and it is safe to say they are valid.
{{- $file := . -}}
{{- $frames := .Site.Params.frames -}}
{{- $datafiles := site.Data -}}
Extracting the needed data
Since data is already extracted from the data files to form content, there is no reason why data designated with a key of “tags” could not also be extracted and used for taxonomical purposes.
To begin the iteration through the data files {{ range frames }}
was used, which should iterate through the frames parameter set in the configuration file. Next, during iteration, the path of the datafile containing the tags needs to be defined. Which once parsed should look like so site.Data.Data1
. Using the simple site.Data.$frame
syntax rasied an error due to the combination of using “.$” together, if the “$” was escaped “.$” this generated an error as well, and placing the entire statement in quotations "site.Data$frame"
also generated an error. Another attempt to find a resolution was using the “printf” funtion {{- $datafile := printf "site.Data%s%s" "." $frame -}}
, but this raised an EOF error.
Likewise the same difficulty was encountered while attempting to define the url to be used to link the tag back to the mainsite. Which once parsed should look like .Site.BaseURL#$frame
which would be translated to https://example.site/#Data1
. Unfortunately, my search for a resolution is what lead me to this forum to discover how to concat a string.
Thank you.
Repo Theme Link
A fork of the theme fresh, Fresh Fork
Hugo Env
hugo v0.127.0+extended linux/amd64 BuildDate=2024-06-15T14:21:13Z VendorInfo=debian:0.127.0-1
GOOS="linux"
GOARCH="amd64"
GOVERSION="go1.22.4"
OptionParser::InvalidOption: invalid option: --embedded
Use --trace for backtrace.
github.com/sass/libsass="3.6.5"
github.com/webmproject/libwebp="v1.3.2"