Comments are rendered THEN stripped (bug?)

Running Hugo Static Site Generator v0.36.1 linux/amd64 BuildDate: 2018-02-15T09:07:45Z

Hi :slight_smile:

According to the docs, HTML comments are stripped from the output by the go template engine. This works as expected.

However, it appears the comments are still rendered before being stripped. I consider this is a bug because this behaviour is not documented, can produce errors but cannot produce any useful result that I could see.

How to reproduce the issue:

<-- {{ partial "test.html" }} -->

Then in partials/test.html:

--> Will this appear?

The result is nothing appears. Please note that in the example provided I tried to close the comment markup in the partial template, to ensure nothing would show up in this arcane setup.

But for example, calling a non-existing template from a comment will raise a build error:

executing "theme/partials/footer.html" at <partial "LOL.html">: error calling partial: Partial "LOL.html" not found

Why do I feel like this is a bug?

As what’s inside the HTML comments never produces anything in the end, I don’t think it should bother to try and render the content. In my opinion, HTML comments should be treated like go templates comments unless specifically told otherwise.

I stumbled upon the issue because I was using HTML comments to comment my templates. I didn’t know about go templates comments before I actually rtfm. I feel like whatever decision you take in regards to this issue, the documentation should be clearer about this behaviour and how to properly comment templates. I will make a pull request when I find an hour :slight_smile:

Also, I realized this may be a “bug” on go template’s side and that’s why I didn’t directly submit an issue on Github.

Thank you for maintaining Hugo, it’s a pleasure to build websites with <3

That’s a feature… what if you have a partial that is a comment block that you want to render as HTML comments?

To comment out any Go Template snippet {{ foo }}, you need to write it as {{/* foo */}}.


Also, from Go template docs:

{{/* a comment */}}
    A comment; discarded. May contain newlines.
    Comments do not nest and must start and end at the
    delimiters, as shown here.

@kaushalmodi is right, this is as designed. I don’t think it’s fair to expect every little thing to be documented. At least not from a open source project.

Thanks for the quick replies!

I don’t think it’s fair to expect every little thing to be documented. At least not from a open source project.

I’m not expecting anything. I’m actually delighted by the thorough documentation that’s already here about many things. Thank you! Am I welcome to contribute a pull-request to the docs on this precise topic, though?

That’s a feature… what if you have a partial that is a comment block that you want to render as HTML comments?

Thanks for the explanation. That’s a use-case I didn’t think about indeed. But I just tried and it does not seem to work. I tried with the following partial:

<!-- {{ add 0 1 }} -->
{{ printf "<!-- %d -->" (add 0 2) | safeHTML }}
{{ "<!-- {{ add 0 3 }} -->" | safeHTML }}
"<!-- {{ add 0 4 }} -->" | safeHTML

And got this result:

<!-- 2 -->
<!-- {{ add 0 3 }} -->
"" | safeHTML

So from what I can see, only the behaviour documented actually works. I also tried piping the partial call through safeHTML itself but it did not change the results (i’m guessing that’s because the partial is rendered BEFORE being piped to safeHTML).

Could you try and show me what you meant?

PRs to the docs repo are always welcomed!

1 Like

This is not a bug. All template files are rendered separate. The partials/test.html is rendered and then added as encoded content to the first line like --&gt; Will this appear?. This then is commented out and removed. Otherwise a sinister theme developer could inject code. This probably does not make much sense in a static templating system, but check the XKCD comic about the same “bug” in MySQL.

https://www.explainxkcd.com/wiki/index.php/327:_Exploits_of_a_Mom

It’s not a bug, it’s a security feature.

I agree (huge xkcd fan here btw). I was not arguing that HTML comments should be rendered, but that they should not be evaluated at all:

I was just wondering what @kaushalmodi meant when they said « what if you have a partial that is a comment block that you want to render as HTML comments? »

As agreed, I PRed to the docs here. Don’t hesitate to adapt my PR to your standards.

Thank you for your fine answers anyway :slight_smile:

To be honest, I assumed that the partials would work in HTML comments. Apologies for not first testing and instead misleading you. I still haven’t have had any time to test this.

I’ll update this thread when I have some info.


In summary, the Go template code has to be commented as per the spec I linked above, and that is well tested :slight_smile: