Rendering error with headless bundle

Dear all,

version used: Hugo Static Site Generator v0.50/extended darwin/amd64 BuildDate: unknown

Following this example in the docs I’m trying to render content from a headless bundle on my homepage (template index.html).

This is my file structure:

|- headless
   |- index.md
   |- test.md

This is my frontmatter from index.md:

+++
title = "Index headless"
description = ""
headline = ""
img = ""
c2a = ""
type = ""
date = 2019-02-21T23:02:14+01:00
weight = ""
draft = true
headless = true
+++

In my index.html template I start with this logic:

{{ $headless := .Site.GetPage "/headless" }}
{{ with $headless }}<script>console.log({{ $headless }})</script>{{ end }}

This is my console.log:

So far ist looks good, the array of page resources is present. But when I add

{{ $reusablePages := $headless.Resources.ByType "page" }}

things break and I get the following error:

Rebuild failed:
Failed to render pages: "/Users/myname/myrootdir/layouts/index.html:4:30": execute of template failed: template: index.html:4:30: executing "main" at <$headless.Resources....>: can't evaluate field Resources in type *hugolib.Page

I’ve tried several other options such as using .Resources.Match (as in the example) but no matter what I try, the error remains. I pretty much have no idea what’s going wrong and would appreciate any help!

This is the complete offending code:

{{ $headless := .Site.GetPage "/headless" }}
{{ $reusablePages := $headless.Resources.ByType "page" }}
{{ with $headless }}<script>console.log({{ $headless }})</script>{{ end }}

You likely have something in your page front matter that is causing your issue (like draft set as true, etc).

I created a sample site with your same content structure. This template code:

{{ $headless := .Site.GetPage "/headless" }}
{{ $headless }}

{{ $reusablePages := $headless.Resources.ByType "page" }}
{{ $reusablePages }}

Gives me this output:

Page(/headless/index.md)

[Page(/headless/test.md)]

Hi zwbetz,

thanks for your constant support here in the forum!

Did you put the code into your index.html template? Based on your reply I’ve found that it works from other templates but not from index.html for the homepage. I wonder if my directory structure could be the reason (there is more than only the offending /headless headless bundle below /content).

Yep I put my code in the index template as well. Here’s my project tree

├── content
│   └── headless
│       ├── index.md
│       └── test.md
├── layouts
│   └── index.html

I’d recommend making a git repo with your project so that we can all be on same page when troubleshooting this.

Unfortunately I can’t. I’m in a company environment, the repo is protected.

Fixed it!

The site is multilingual. .md files for the secondary language where missing :man_facepalming:

Once again: thank you!