Get variable by string parameter

Is there a way to add the name of a variable to the configuration and then retrieve that variable in a layout? In this specific case (would want to know a very generic way though) I want to configure for instance, what slice or dict to range through inside of a footer sidebar.

The idea is this:

config

footerlists = ["list1", "list2", "list3"]

and in the footer:

{{ range $config.footerlists }}
{{ range ( getDict . ) }}
do something
{{ end }}
{{ end }}

A very rudimentary way would probably involve the index function but in that case I would need to know what the “root” of the index (site, a menu, whatever) should be and that might differ?

I don’t know if I understand the question completely, but you could create a table array in your config:

[[params.footers]]
name = "one"
option = "hello"
[[params.footers]]
name = "two"
option = goodbye

And then range through it and add a parameter to test for what you need:

{{ range .Site.Params.footers }}
    {{ if eq .name "one" }}
        {{ .option }}
    {{ end }}
{{ end }}

Is this what you were looking for?

Nope. More like this (non working):

{{ range .Site.Params.footers }}
    {{ range makethisamap(.option) }}
        // do more stuff
    {{ end }}
{{ end }}

with

[[params.footers]]
option = "site.Menus.main"
[[params.footers]]
option = "some-other-variable"

The problem I am having is to apply an available variable while having the “reference” (if that is the term for that) only as a string.

The other version you suggested I have working at the moment, but for it to work it all needs to be for instance defined as menu in the configuration. I want to vary between configs and menus and maybe a getPage functionality (for some logo/text in the footer or sidebar) or basically anything.

Index, Where, and Scratch

I can only think of a few built-in ways to retrieve stored values based on a string: index, where, and Scratch, or some combination thereof.

{{ $employees := slice
  (dict "id" "001" "name" "Bob Smith" "age" 23)
  (dict "id" "002" "name" "Kate Jones" "age" 29)
  (dict "id" "003" "name" "Addison Parks" "age" 31)
}}
{{ .Scratch.Set "employees" $employees }}

Then retrieve the name of employee 002 with:

{{ index (where (.Scratch.Get "employees") "id" "002") 0 "name" }}

Maybe you can put everything you might need into page-scoped scratches?

Index Equivalents

One thing I sometimes forget about index… these are equivalent:

{{ site.Params.foo.bar.baz }}
{{ index site.Params.foo.bar "baz" }}
{{ index site.Params.foo "bar" "baz" }}
{{ index site.Params "foo" "bar" "baz" }}

Example

git clone --single-branch -b hugo-forum-topic-32412 https://github.com/jmooring/hugo-testing hugo-forum-topic-32412
cd hugo-forum-topic-32412
hugo server

Files of interest:

  • config.toml
  • layouts/_default/baseof.html (scratches set here)
  • layouts/partials/footer.html (putting stuff into maps)
  • layouts/_default/single.html (calls the partial)

View the bottom of any of the content pages to see the two jsonified maps.

The structures of the two maps are different, which will be a challenge if you blindly range through your list of collections. If you need to normalize the structures, you could do it in the partial, or when you create the scratches.

I think this approach is a bit fragile and has a lot of moving parts, but I couldn’t come up with anything better.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.