gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
76.2k stars 7.55k forks source link

Support `.gohtml` file extension for templates #10449

Open pmwmedia opened 2 years ago

pmwmedia commented 2 years ago

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.

SbstnErhrdt commented 1 year ago

That would be very useful indeed.

Pr0Ger commented 1 year ago

Looks like a duplicate of #3230

nk9 commented 1 year ago

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 commented 11 months ago

Any update on this? Tagging @jmooring for visibility.

benjaoming commented 5 months ago

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

bep commented 5 months ago

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",
      },
    },
  ],
}
nk9 commented 5 months ago

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.

benjaoming commented 5 months ago

@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.