How to add comments in a template?

I’m stuck while trying to do a very easy thing: I need to comment a line in a template file (specifically layouts/index.html).
As far as I can see from golang template, I should use //, but I get this error:

Rendering error: html/template: "index.html" is an incomplete template

I tried also HTML and CSS comments but it didn’t work.

//test test

Works fine for me (tested in index.html and layouts/_default/single.html).

Update: Uhm yeah it’s “working” in the sense that Hugo isn’t crashing, the comments are just shoved into the output, so nevermind my reply above :slight_smile:

You want

{{/* comments here */}}

also works multiline.

Most of the template rules are here: http://golang.org/pkg/text/template/

9 Likes

Ok, this means that {{ cannot be commented out?

What I actually want is commenting out a line to test different ideas without deleting anything, e.g.:

{{ range where .Data.Pages.Reverse }}
{{ range where .Data.Pages.ByDate "Section" "events" }}

@natefinch what you suggest is the best way to do so? (I want to be sure that you understood my use case).
Many thanks

You can use it to comment out blocks of whatever:

{{/*
 
{{ . }}
 
*/}}
10 Likes

On a similar note, is there a way to stop Hugo from stripping out HTML comments…

<!-- blah blah blah //-->

…when rendering template code?

Take a look at the Go Templates Primer page for how to preserve comments.

Oh. Ta! –I must have speed read past that bit

For others wondering the same thing, the TL;DR; version is to use the following format:

{{ "<!-- blah blah blah //-->" | safeHtml }}

Which is a bit clunky in my opinion. T’would be nice if there was config toggle a la --buildDrafts for this, such as --stripComments=false

4 Likes

Quick follow up on this:

It seems both traditional HTML <!-- blah blah blah //--> and Go Template style coments {{ "<!-- blah blah blah //-->" | safeHtml }} get stripped out of shortcode templates.

Actually,on further inspection, that’s not strictly true. The youtube.html and vimeo.html shortcodes preserve comments written using the go template syntax, whereas thre img.html shortcode strips them out in whatever format they’re written.

Phew! –a bit of consistency might be nice here!

@stiobhart what Hugo version do you use now?

Well that depends on who you ask. If I ask Homebrew [which I installed Hugo with], it thinks it’s Version 0,12:

hugo: stable 0.12 (bottled), HEAD
http://hugo.spf13.com/
/usr/local/Cellar/hugo/0.12 (5 files, 22M) *
  Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/hugo.rb````

But if I ask Hugo itself, it seems to think it's Version 0,13DEV:

````$: hugo version
Hugo Static Site Generator v0.13-DEV````

Can you run which hugo? And can you confirm that you’re running it by typing in hugo at the command prompt?

Yep. It’s the same one. Which Hugo gives:

/usr/local/bin/hugo````

which is Homebrew's symlinked one:

````$: ls -al /usr/local/bin/hugo*
lrwxr-xr-x  1 madra  admin  28 14 Dec 21:08 /usr/local/bin/hugo -> ../Cellar/hugo/0.12/bin/hugo
lrwxr-xr-x  1 madra  admin  33 14 Dec 21:08 /usr/local/bin/hugo-0.12 -> ../Cellar/hugo/0.12/bin/hugo-0.12````

Ah... wait a min. Looks like for some reason Homebrew as installed ````0.13DEV```` as plain ol' "hugo" and ````0,12```` as "hugo-0.12", so I should be able to explicitly run 0.12 by using *hugo-0.12* as the command, rather than *hugo*.

I wonder how that happened?

**UPDATE:**

OK. Just to confirm, neither *0,12* or *0,13-DEV* preserve comments in the *img.html* shortcode template

homebrew uses Git for version control, and the master branch is the WIP / 0.13DEV. The other one must run off of the last tagged release of 0.12.

1 Like

I have now tested it with the latest 0.13-DEV and it works fine for me (that is, the comments are preserved).

I haven’t tested it with 0.12, but I understand why it wouldn’t work there.

The shorctode handling has been rewritten in 0.13. In 0.12 it would go twice through template/markdown processing, and the comment would survive once, but not twice … In 0.13 this shouldn’t be an issue.

EDIT IN: Dont’ ask me aboout Brew/Homebrew, I have no clue. I’m a Linux person.

Is there any reason why HTML comments are stripped out?
I really don’t understand the logic

@Frederic

I think this is a question for the Golang team, but my assumptions/thoughts would be:

  1. Comments are for devs and not for the website audience
  2. It’s always a goal to keep things small before sending them over the wire, so why include notes that take up valuable bytes?
  3. You can still see them in source, so they’re always there for you, the developer.
  4. As mentioned in this thread, you can still include them if you really want to…

What would be the logic of keeping them in?

I have some answers

  1. Comments are good! The average site audience doesn’t see the comments. The persons who see the comments are interesting in the way the things are build, so that’s clearly for them.
  2. Websites are full of images, audios and videos. The textual comments will not add lots of bytes.

Why the comments missed me?
I tried to undestand which of the theme’s source was used for my index.md pages. So I added some comments in the .htlm sources in the theme’s directory to identify them.
I lost lot of time trying to identify the good source, before seing this post and understanding that the comments where just deleted.

Right. That’s why they are in source and stay in source. You can still keep them in your rendered website if you really want to.

Comments are still in source, but you might want to reach out to the theme’s developer if you need more clarification.

Let’s just agree to disagree on this one :smile: Glad you got it all figured out!

I had to range over a huge CSV file > 50K lignes.
I had to make comment (html style for colorization in my editor) in my code and wanted to remove the unnecessary white lines who bloated the resulting HTML

This worked like a charm.

{{- /* <!-- My comment --> */ -}}