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:

  author = "Lorem"
  description = "Lorem ipsum"
  docsTheme = "docs1"

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

  image: "assets/img/someImage.png"

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

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


{{ .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?


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.


{{ $docsData := index $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 $docsTheme "logo" "image"}}