Build Websites with Hugo: Code in chapter two won't run

I’m working through Mr. Hogan’s book, and the code has been running fine, until I got to the section “Using Content Blocks and Partials” in chapter 2. When I substituted the following code for ‘index.html’, I got a blank output:

{{ define “main” }}

{{ .Content }}

{{ end }}

Hugo didn’t give any error messages, just blank output. If I commented out the above code, and restored the previous html, I would get output. Also, I found I would get wonky results if I copied and pasted from the PDF, so I typed this code by hand, and still no output. What do I need to do to get output from Hugo using ‘partial’ blocks?

I have installed Hugo 0.77 on Windows 10 (64-bit). Thanks!

-Chris Adams.

Hi Chris. Thanks for reading.

I just went through chapter 2 again with the latest Hugo and was able to follow the book, but I have had some others have interesting issues happening.

First, what text editor are you using? I have seen some text editors with “auto formatting” tools (VSCode) make really subtle changes to Hugo template files. Make sure that when you save you don’t see any spaces introduced or removed. Someone struggled for days with that till we figured it out.

But beyond that…

{{ define "main" }}
  {{ .Content }}
{{ end }}

is correct for that page, provided it’s in themes/basic/layouts/index.html.

That should be the only content in the file though. All the rest of the content should be gone.

What’s your themes/basic/layouts/_default/baseof.html file look like? We might find an issue there.

Hi Brian –

Thanks for responding! You wrote:

“That should be the only content in the file though. All the rest of the content should be gone.”

Actually, I had a comment in the file, along with the code I provided above. And when I removed that comment, the output magically reappeared! I really had no idea that the presence or absence of a comment would make such a difference.

Do other files in Hugo respond this way to comments? I may have to do some experimenting to find out. In any case, thank you for taking the time to respond; it really helped!

-Chris Adams.

1 Like

Hey Chris. I’m really glad I could help.

As for comments, you can have certain kinds of comments in some of your files, but Hugo really hates HTML comments if they are at the top of the file in your templates.

It’s a known issue: Comment in first row of templates makes the page go blank · Issue #6816 · gohugoio/hugo · GitHub

I’d hoped it’d been resolved by now. But… nope. Sorry!!!

This solved the problem for me as well. My comment was at the end of the file.

FWIW, the code still ran correctly even with the formatting (indenting) of the index.html file being slightly different than that shown.

Thanks for finding the solution.

Had same issue. Removing comments resolved.

FYI: Comments in the *.html files continue to terrify Hugo. It silently generates empty output. Bad new for those who like to document their code…