How to add comments in a template?

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 --> */ -}}

Why would you put HTML comment inside the Go template comment block… even this (yes, multi-line!) works great:

{{/* my 
multi-line
comment */}}
{{- $foo := "whatever non-comment stuff that follows" }}
1 Like

Oh, as i said, just because my editor VSC (visual studio code) colorize only HTML and not the hugo comments. Just that :frowning:

Edit : If I had the go-html-template lexer colorisation results in VSC I would be happy …

Ah, OK, sorry, missed that :slight_smile:

No problem at all @kaushalmodi … thank you for your comment, makes me think I should investigate in colorizing my code instead of changing it :slight_smile:

Edit : Works fine if I manually change the file type for template from HTML to Go … Have to do it each time, but it’s nice. Thanks for the remark, I (re) changed it for a cleaner syntax.

That’s where workspace settings come in handy. In your project’s root create a folder .vscode with a file called settings.json. To associate *.html files with gohtml add:

{
  "files.associations": {
    "*.html": "gohtml"
  }
}
3 Likes

Go HTML is an addin for VS Code. My html template files were defaulting to Handlebars which isn’t that bad (that’s because I already had the handlebars addin installed). But I’ve just installed the Go Templates addin and it does look a little better.

As @holehan points out, you can use workspace settings to make this the default for just your Hugo projects.

If you don’t already have it, you might want to install the “Better TOML” addin as well. Also “hugofy” has some actions like creating posts, sites, etc.

Some markdown addins are also useful.

1 Like

You can, of course, also do settings at the folder level.

1 Like

Whoaaa …
gotemplate-syntax plugin + .vscode/settings.json trick works like a charm. Very nice colorization.
Thanks a lot all @holehan @TotallyInformation @kaushalmodi

1 Like