At <.Parent.Dir>: can't evaluate field Dir in type page.Page

I just installed the latest Hugo version:

hugo v0.97.3-078053a43d746a26aa3d48cf1ec7122ae78a9bb4 windows/amd64 BuildDate=2022-04-18T17:22:19Z VendorInfo=gohugoio

When I generate the site I get the error(s):

execute of template failed: template: partials/prints-sidebar.html:7:51: executing “partials/prints-sidebar.html” at <.Parent.Dir>: can’t evaluate field Dir in type page.Page

It seems to be in the next part of the code:


{{- $path := (print .Parent.Dir) -}} ← This is line 7
{{- $prints := where .Site.Pages “Section” “artists” -}}
{{- range $prints }}
{{- if hasPrefix .Dir $path }}

With a previous version 0.91.2 I didn’t have this problem. What do I need to change?

{{ .Parent.File.Dir }}

Try adding .File.Dir instead. File Variables | Hugo

If I use this, I get the error:

at <.Dir>: can’t evaluate field Dir in type page.Page

                    {{- $path := (print .Parent.File.Dir) -}}
                    {{- $prints := where .Site.Pages "Section" "artists" -}}
                    {{- range  $prints }}
                        {{- if hasPrefix .Dir $path }} <-- Now an error at this line

I tried this one, but now I get:

at <.Dir>: can’t evaluate field Dir in type page.Page

                    {{- $path := (print .File.Dir) -}}
                    {{- $prints := where .Site.Pages "Section" "artists" -}}
                    {{- range  $prints }} <-- Now I get an error at this line
                        {{- if hasPrefix .File.Dir $path }}

You are editing the code incorrectly. Try this

{{- $path := (print .Parent.File.Dir) -}}
    {{- $prints := where site.Pages "Section" "artists" -}}
         {{- range  $prints }}
          {{- if hasPrefix .File.Dir $path }}

Thnx for the help. But in

                    {{- $path := (print .Parent.File.Dir) -}}
                    {{- $prints := where site.Pages "Section" "artists" -}}
                    {{- range  $prints }} <-- Now I get the error here :(
                        {{- if hasPrefix .File.Dir $path }}

at <.Dir>: can’t evaluate field Dir in type page.Page

I tried a lot of options and looked in the documentation, but didn’t find a solution. I switched back to the older version.

I had a similar error recently and .File.Dir worked for me. The change was introduced in this version. Share your repository and the error you are getting now and perhaps @jmooring can help you.

Fixed!!!
For some reason the error didn’t report the correct file/line number. There was an other file where I also needed to change to .File.Dir.
Thnx for the hints

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