Can't evaluate existing field - partial content

Hello.

I’m having a very strange error with data types.

At my company we are trying to make pages with partials (blocks for us).

This is our front matter page1.md, where we define inside [[blocks]] some variables and configuration. Also we define [blocks.navbar] that include another partial inside the first one.

title = "Design"
type = "page"
[[blocks]]
	description = ""
	template = "hero"
	[blocks.navbar]
		"class" = "is-transparent"

Then we have blocks.html that has a Range function that include every partial defined inside [[blocks]] in the frontmatter, into the specific page and pass throught context the variables defined inside [[blocks]]

{{ $site := .Site }}

{{- range .Params.blocks -}}

  {{ $context := (dict "Site" $site "Block" .) }}
  {{ $block_url := print (delimit (slice "blocks/" .template ) "") }}
  {{ partial $block_url $context }}

{{- end -}}

The problem comes when I try to acess .Params.blocks.navbar.class. If inside the Range function I add this line {{ printf "%#v" .navbar.class}} then the build fail with this error.

executing "partials/blocks.html" at <.navbar.class>: can't evaluate field class in type interface {}

At that point, inside the Range function, the data type of .Params.blocks.navbar is map[string]interface {}{"class":"is-transparent"}. Idk why i can’t access class value, with .class notation.

The crazy part comes when I try to imitate the map[string] interface, building a custom variable, with the following code and then try to print the content…

{{ $navbar_config := (dict "class" "is-transparent") }}
{{ printf "%#v" $navbar_config}}

I got the same data type as shown before, map[string]interface {}{"class":"is-transparent"} but I can access the class field with no error

{{ printf "%#v" $navbar_config.class}}

result in “is-transparent”.

I’ve search the forum and the issues, but find nothing that helps me solve the problem. Idk if this is part of Hugo or is something weird happening in Go.

FYI: this is my env.

Hugo Static Site Generator v0.49.2/extended linux/amd64 BuildDate: unknown
GOOS="linux"
GOARCH="amd64"
GOVERSION="go1.11.1

with range the context switches to the iterated field {{.}}, for accessing anything outside you must use $.anything

ex: .Params.blocks goes to $.Params.blocks

hope this helps

@ju52 is right!

Care to read more about this, enjoy this shameless plug: https://regisphilibert.com/blog/2018/02/hugo-the-scope-the-context-and-the-dot/

Hey guys, thanks for your answers.

I you look at this part of the post,

If inside the Range function I add this line {{ printf "%#v" .navbar.class}} then the build fail with this error.

I’m noyt trying to access external context from inside the range function. I’m trying to get .navbar.class field, that is inside .Params.Blocks or {{ . }} inside range.

This is how the code is failing

{{- range .Params.blocks -}}

 {{ printf "%#v" .navbar.class}}
 ...result in error : can't evaluate field class in type interface {}

{{- end -}}

Are you sure all of your pages have blocks set and all of those blocks have .navbar set and all of them have navbar.class ? If only one of them don’t, this will break.

Also, do you have a repo :smiley: ?