Comments and unused partials still try to build

Since I’m here, I might as well ask about these two strange occurrences.

Partials attempt to execute even when not called.
This one confuses me. I have a partial that I am working on building for a theme. It has known errors in it, so to get the site to build I have commented out or removed the line that calls it. Yet, I am still getting the error:

parse failed: template: partials/api/responses.html:6: undefined variable "$data"

Of course it’s undefined. I haven’t called the partial. But unless I delete the partial, I cannot build the site to test.

Comments don’t comment anything but themselves
So I tried to comment out the file, by wrapping it in:

{{/*
{{ other template commands here }}
*/}}

But that doesn’t work, because Hugo is still trying to interpret everything inside of the comments. So they seem to be useless unless you go through and add them to every single occurrence of {{ and }}, which for a large chunk of code is just not feasible. Is this expected behavior, and is there any way to comment out code other than removing it?

Apologies if any frustration is making its way into these questions. I’m in debugging hell at the moment.

partials: “parsing” is not “executing”. Hugo parses all templates at the beginning, If you have a syntax error or any other error it will fail. Without seeing your particular setup it’s hard to say if your partial is executed or something else is the error. The followup question shows, that “commented out” might not have been really commented out. So that might be the reason for the error.

comments: yep. There is NO comment to comment out multiple gotemplate tags. You need to comment out every single gotemplate tag. {{/* is the comment, so {{/* other template commands here */}} is the way to comment. Some IDEs with gotemplate support know that and do accordingly if you mark many tags and set to commented.

1 Like

Thank you for the answers. Frustrating that there’s no simple way to extract blocks of code without outright deleting them, but I guess that’s what we’re stuck with.

I do not understand this statement. Please elaborate.

{{/*
{{ some tag }}
{{ some other tag }}
*/}}

does not work

{{/* some tag */}}
{{/* some other tag */}}

does work. in sense of “commenting out” the tags.

Am I wrong? (Little bit afraid, because YOU ask that question)

To expand:

<!--
<something>
<somethingelse>
//-->

would comment out multiple HTML tags. It seems there is no way to do something like that in gotemplates.

{{/*
  {{ $a := substr "foo" 1 2 3 }}
  {{ $b := substr "foo" }}
*/}}

The first statement has too many args.
The second statement has too few args.
Please test.

1 Like

Weird. It works as I would hoped it would. But I remember cases, where Hugo complains about things inside of {{/* */}} comments. Testing frantically now. Could it be that is is a recent fix/feature?

I don’t think so. See https://golang.org/pkg/text/template/#hdr-Actions.

A comment; discarded. May contain newlines.

Perhaps you are remembering an instance where a multi-line comment was prematurely closed. For example, this will generate an error:

{{/*
  {{ $a := substr "foo" 1 2 3 }} */}}
  {{ $b := substr "foo" }}
*/}}
1 Like

I’m slightly worried about my mental faculties here now. Maybe the IDE did show errors by fault, but I am pretty sure that a running Hugo server complained to me about Go-commented Go-template-tags… I spent much time commenting every single tag when I commented out Go code all the time :frowning:

Hmm, maybe it was a connection of HTML and Go comments… I’ll have to look into this.

Other than that: WHY is the OPs code still parsed with comments around it. Maybe if we can get a sample of @mwaterous’s code it get’s clearer. Maybe it’s one of these cases that made me think it’s not supposed to work?

No errors in all versions. Even if I have two opening or closing comment brackets it just shows them :] weird.

1 Like

I swear it wasn’t working. I was getting errors in one of my partials, so I tried to wrap the entire partial in comments until I could debug the error, and it continued to spit out the exact same error. You know what, looking at it again, there was another comment in the file, that probably terminated the first opening comment and the build was breaking on the remainder.

Gawd it’s been a long day. Thanks for all the help @davidsneighbour and @jmooring

1 Like

Yep, comments inside of comments lead to errors:

{{/*
{{/* test */}}
{{< quote >}}bla{{< /quote >}}
*/}}

This one errors out on the < of the third line interestingly:

ERROR 2020/12/17 15:47:45 Process: loading templates: "filepath.html:3:1": parse failed: template: filepath.html:3: unexpected "<" in command

and this is parsed without errors:

{{/*
{{/* test */}}
*/}}

I’ll go back to paper notes :smiley:

https://golang.org/pkg/text/template/#hdr-Actions

Comments do not nest

1 Like

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