Concatenating variables

I’m currently building a theme, and I would like to be able to read a site-wide param that defines one styling option for that theme, but then use it within my individual layouts to read matching data files.

For example, if this is my config.toml:

[params]
  author = "Lorem"
  description = "Lorem ipsum"
  docsTheme = "docs1"

And I have a matching data file at data/docs/docs1.yaml that contains:

logo: 
  image: "assets/img/someImage.png"

I want to know if it’s possible to do something like this:

{{ $docsTheme := .Site.Params.docsTheme }}
{{ .Site.Data.docs.$docsTheme.logo.image }}

or

{{ .Site.Data.docs.( .Site.Params.docsTheme ).logo.image }}

Both of these don’t work, and an easy solution would be to modify the name of the logo image to something like docs1.png and access it that way, but I’m sure to something I will run into similar as I go along. Is this possible somehow?

Thanks.

Try it

The clue lies in the “index func”. (it’s in the documentation).

A related tip that even I didn’t realize until recently is that the index func can take a list of indices and you can mix and match array and map indices.

E.g.:

{{ $docsData := index .Site.Data.docs $docsTheme }}
{{ $logo := $docsData.logo }}

I don’t think you can do $docsData.logo.image, but If you need to access the logo image in one go, do this:

{{ $logo := index $docsData "logo" "image" }}
1 Like

Thank you @bep! I was circling around the index docs as you responded.

I ended up going with

{{ $logo := index .Site.Data.docs $docsTheme "logo" "image"}}
2 Likes