hugo icon indicating copy to clipboard operation
hugo copied to clipboard

Support `.gohtml` file extension for templates

Open pmwmedia opened this issue 3 years ago • 15 comments

For partials and shortcodes it is already possible with the current version of Hugo to use the file extension .gohtml instead of .html. It would be great if the file extension .gohtml could also be used for templates.

The advantage of .gohtml instead of .html is better syntax highlighting for Golang in many IDEs like IntelliJ, without the need for manual hacks.

pmwmedia avatar Nov 17 '22 18:11 pmwmedia

That would be very useful indeed.

SbstnErhrdt avatar Jun 08 '23 06:06 SbstnErhrdt

Looks like a duplicate of #3230

Pr0Ger avatar Jun 30 '23 08:06 Pr0Ger

That other issue has now been closed and locked, but remains very much relevant. We need a way to name template files so that syntax highlighting can know they are Hugo templates.

nk9 avatar Jul 22 '23 04:07 nk9

Any update on this? Tagging @jmooring for visibility.

nk9 avatar Dec 16 '23 16:12 nk9

I would support this as an optional feature, but I don't find it suitable as a new convention.

I think it's fairly common that template files are just named by their intended format. For instance, Django templates are also normally called .html.

Is there any existing file extension for Go templates, is .gotmpl a thing?

My worry is that .gohtml is great for an editor that supports it... but other editors may fail to even do basic .html highlighting. And in some cases, the user doesn't have real Go Template highlighting, so they have to select HTML and get the same result as when the file was called .html.

In these cases, use experience is worse. This is also relevant for displaying/previewing code on Git platforms like GitLab, Gitea, Forgejo and their mega-sibling GitHub.

IntelliJ IDEs are smart enough to force a choice with a modal when it doesn't understand the file extension:

image

But other editors will likely just not syntax highlight at all. Here's gEdit for instance:

image

benjaoming avatar Jun 28 '24 14:06 benjaoming

The advantage of .gohtml instead of .html is better syntax highlighting for Golang in many IDEs like IntelliJ, without the need for manual hacks.

The template file extension in Hugo is directly connected to the MIME type definition used in layout resolution for a given output format, so any costly special handling of .gohtml needs stronger arguments.

Prettier with the go-template do a great job with Go/Hugo templates, and is easy to set up for HTML files. You could restrict this to the layout folder(s) etc.

{
  overrides: [
    {
      files: ["*.html"],
      options: {
        parser: "go-template",
      },
    },
  ],
}

bep avatar Jun 28 '24 15:06 bep

What if we used .go.html? Then it would default to something useful in the common case, but the extension would be available for keying off of in the case that language extensions wanted to provide more intelligent highlighting/etc.

nk9 avatar Jun 28 '24 18:06 nk9

@nk9 I think in that case, it would be easier to promote a convention for themes to follow. In IntelliJ, wild card patterns define a syntax highlighting, so it's possible to use *.go.html.

benjaoming avatar Jun 28 '24 21:06 benjaoming

+1 for an infix, but it should be *.gotmpl.html in my opinion because Go (implied by *.go.*) and Go templates (implied by *.gotmpl.*) have different syntax.

The infix would need to be excluded from the template name to not cause issues with resolving templates and detecting changes in live-reload mode (e.g. mytemplate.gotmpl.htmlmytemplate), and maybe not interfere with template lookup order.

I was looking into this myself (though not familiar with codebase) but it might be easier once #13515 goes through (assuming it will).

jenbroek avatar Mar 26 '25 14:03 jenbroek

What about making it configurable, and follow the decision made by the Go team for gopls? https://github.com/golang/go/issues/36911#issuecomment-961245361

It's fun to bikeshed about the most correct extension format, but the decision may go against the mainstream. YAML has .yaml as an official extension, and the majority of people still uses .yml, like they're still using DOS or something.

prochac avatar Jun 16 '25 21:06 prochac

I personally just bumped into this issue using Webstorm.

@bep There is an inconsistency where for partials it works and for layouts it does not. I agree that .gohtml does not need special treatment, but .html is the one getting special treatment by hugo.

Hugo will properly pick up a layout with the .html extension, but the IDE will read the file as an .html file and mark any go templating syntax as an error.

Hugo will not pick up a layout with the .gohtml extension, but the IDE will provide proper syntax highlighting.

Hugo will pick up partials with the .gohtml extension when referenced by path, e.g. {{ partial "path/custom_path.gohtml" }}

I am fairly new to hugo, I might have missed or misinterpreted its workings, but I expected layouts to just work with a .gohtml extension because it worked for my partials.

shiouen avatar Jun 17 '25 15:06 shiouen

@shiouen

using Webstorm

I'm not a Webstorm user, but it seems like this would work: https://www.jetbrains.com/help/webstorm/creating-and-registering-file-types.html#change-extension

In VS Code with the Go Template Support extension I do this:

"go-template.patterns": [
  "**/*.html"
]

{{ partial "path/custom_path.gohtml" }}

Don't do that. It may work today, but it is not future proof.

In relation to some of the suggestions in this thread:

  • Changing or allowing an alternate extension is not going to happen.

    The template file extension in Hugo is directly connected to the MIME type definition used in layout resolution for a given output format.

  • Inserting something before the final dot (e.g., go, gohtml, gotmpl, etc.) is not a trivial change. Template file names already include several identifiers (bits between dots) including page kind, layout name from front matter, language, output format, and media (MIME) type. For example:

    myshortcode.section.mylayout.en.rss.xml
    

    The example above is an XML file with Go template code.

jmooring avatar Jun 17 '25 15:06 jmooring

@shiouen

using Webstorm

I'm not a Webstorm user, but it seems like this would work: https://www.jetbrains.com/help/webstorm/creating-and-registering-file-types.html#change-extension

In VS Code with the Go Template Support extension I do this:

"go-template.patterns": [
  "**/*.html"
]

{{ partial "path/custom_path.gohtml" }}

Don't do that. It may work today, but it is not future proof.

In relation to some of the suggestions in this thread:

  • Changing or allowing an alternate extension is not going to happen.

    The template file extension in Hugo is directly connected to the MIME type definition used in layout resolution for a given output format.

  • Inserting something before the final dot (e.g., go, gohtml, gotmpl, etc.) is not a trivial change. Template file names already include several identifiers (bits between dots) including page kind, layout name from front matter, language, output format, and media (MIME) type. For example:

    myshortcode.section.mylayout.en.rss.xml
    

    The example above is an XML file with Go template code.

Hi @jmooring

I see, since my partial usage is not as intended I will revert back to .html extensions.

The link you sent me is indeed where you can associate file types, but Jetbrains forces a specific wildcard to be assigned only once. By default the IDE assigns *.html to HTML, which is fine. The IDE will not allow me to assign the *.html wildcard a second time with Go template files. As a result, the templating syntax goes unrecognized by the IDE as long as the .html extension is used. VS code seems to have more options there.

Thanks for getting back to me, appreciate it.

shiouen avatar Jun 18 '25 03:06 shiouen

@shiouen I just installed Webstorm and the Go Template plugin.

Then I went to Settings > Editor > File Types and removed *.html from the list of extensions associated with the HTML file type:

Image

Then I added *.html to the list of extensions associated with Go template files, and set the template data language[^1] to HTML.

Image

Image

Works great.

Image

And, since the template data language is HTML you can use this setup for every HTML file, not just those that contain Go template syntax.

[^1]: the language used within a template's static content

jmooring avatar Jun 18 '25 05:06 jmooring

It works, but this makes all HTML files go template files across all my other projects too. Templating syntax would be accepted by the IDE, even where it does not belong. HTML file icons would all be replaced with gophers, even if they do not contain templating syntax. I don't think there is a straightforward solution for Jetbrains users without tradeoffs and it's a Jetbrains IDE issue.

Currently I renamed the files to use .go.html and associated the file type with go templates, which is definitely also a workaround and probably not future proof, but it's consistent and does not affect my other projects.

shiouen avatar Jun 18 '25 17:06 shiouen

@shiouen This was surprising, and not in a good way: https://youtrack.jetbrains.com/issue/IDEABKL-6335/Assign-file-types-not-only-globally-but-project-wide-as-well

On the off chance that this was simply a UI limitation I created my-project/.idea/filetypes.xml... no joy.

With respect to using a .go.html extension, this doesn't work unless you set the defaultContentLanguage to go:

layouts/
├── baseof.go.html
├── home.go.html
├── page.go.html
├── section.go.html
├── taxonomy.go.html
└── term.go.html

jmooring avatar Jun 19 '25 14:06 jmooring