Construct a partial call with Data

Hello,

I’m trying to make a call from within a TOML file data, like so:

[featured]
current = "peter"

[featured.peter]
img = "/avatar.jpg"
   [featured.peter.btn]
      img = "btn.jpg"
[featured.rose]
img = "/avatar.jpg"
   [featured.rose.btn]
      img = "btn.jpg"

What I want is to change the value of current manually for anything I want, so when I call:

{{ .Site.Data.users.featured.current.btn }}

It displays the button info for rose, or peter, or anyone.

Problem is, as you might have guessed, that it shows me “peter” instead of the map.

My idea is to change current = “peter” and that the changes occur everywhere instead of manually changing all those calls.

{{ .Site.Data.users.featured.$current.btn }}

Doesn’t appears to work either, I can’t mix that into a variable.

Any ideas?

Use the index function.

Example from my theme: TOML data | template using index

Tweak in the template to make clear what’s happening:

(index $.Site.Data.svg "mastodon")

That "mastodon" string could have been "mastodon-alt" too, based on the value of (.svg | default (.name | lower)) in the template.

thanks for that, kaushalmodi, I was struggling really hard with it. You showed me the path.

Have a cookie > https://media.giphy.com/media/1ngQorBCDcUFy/giphy.gif

Thanks! :smile:

1 Like