Hugo: How to write a comment in Hugo code

Published: 2020-01-20 19:07:17 -0500 -0500

Reading time: 1 mins (163 words)

Tags: hugo

problem

I’m using the static site generator Hugo to build my sites but sometimes the templating can get kinda complex. On these occasions it’d be nice to be able to comment my code so I could more easily remember what parts were trying to do what. How do I write comments with Hugo?

solution

To write comments in Hugo, you can simply use HTML comments. For example:

<!-- I am a Hugo comment -->

HTML comments are stripped by default (though don’t go adding Go templates to them as they’ll still be evaluated).

Here’s another example of a comment in Hugo that (at time of writing) I have live on my blog:

<!-- if we didn't find any relevant content in tags, we look in kind -->
{{ if not ($scratch.Get "relatedContent") }}
    {{ $scratch.Set "relatedContent" (where .Site.RegularPages "Type" .Section) }}
{{ end }}

I’ve confirmed that this comment doesn’t appear in the output html that’s served to visitor’s browsers on Hugo v0.59.1.

About the Ham

Hi I'm Hamilton and I wrote / built this! I like doing lots of things, but mostly I like to build (and then share those buildings). Some things I'm currently building: art, software, and prose. To stay up-to-date with my latest buildings, subscribe to my email list or follow one of my many other accounts. #buildit #shipit #justhamit

comments powered by Disqus