Hugo

Can I convert string into an object named the same as that string value

Can I convert string into an object named the same as that string value?
For example, suppose I want to access .Site.Data.slider2 instead of slider using my $foo string.
‘’’
{{$foo := “slider2”}}
{{ if .Site.Data.slider }} <!-- load slider —>
‘’’
Please let me know if this is possible and how.
Thank you

You can use the index function: https://gohugo.io/functions/index-function/

Thank you pointfar. I made a dictionary out of my data parameter and used it to load the data, since I don’t want the shortcode user to be bothered with the dictionary.

{{ $datadict := (dict "data" (.Get "data")) }}
{{ $.Scratch.Set "site_data_slider" (index .Site.Data ($datadict.data)) }}

Reference: https://discourse.gohugo.io/t/dynamically-load-datafiles-based-on-variables/9349/2