How to read map in hugo

Hello all,

I have set up multiple languages for hugo.

config.toml

[languages]
  [languages.en]
    [languages.en.params]
    [[languages.en.params.address]]
      email = "contact@domain.com"
      address = "RM 99, No. 87, West Road, LA, CA, USA"
  [languages.es]
    [languages.es.params]
    [[languages.es.params.address]]
      email = "contact@domain.com"
      address = "1234"

And I read it in html file

{{ $address := .Site.Params.address }}
{{ $address }}

It prints [map[address:RM 99, No. 87, West Road, LA, CA, USA email:contact@domain.com]], I tried to use {{ $address.email }} and {{ $address.address}} to get the value of each parameter, but I got an error

execute of template failed at <$address.email>: can’t evaluate field email in type []interface {} render of "page" failed: can’t evaluate field email in type []interface {}  failed to render pages: can’t evaluate field email in type []interface {} 

How to fix this? Thank you.

That won’t work. You have to call each field individually, e.g {{ $address := .Site.Params.address.email }}

@tut Thank you.

I changed the code to

{{ $address := .Site.Params.address.email }}
{{ $address }}

But I still get the same error

execute of template failed at <.Site.Params.address.email>: can’t evaluate field email in type []interface {} render of "page" failed: can’t evaluate field email in type []interface {}  failed to render pages: can’t evaluate field email in type []interface {} 

The field can’t be called.

The double brackets makes an array with toml.

2 Likes

@frjo Thank you so much. That’s really my typo.

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