Template extension aliases (or variants)

Hello and Happy New Year everyone :slightly_smiling_face:

I am really excited to have the chance to propose a feature—a special case of which I have seen discussed here and there on the Internet, and even on this forum. It is possible to find Hugo users asking to allow layouts to have the *.gohtmlextension, mainly due to formatting issues. In fact, my initial motivation was exactly the same, but I think this potential feature can be generalised in a quite useful way.

What I propose is to define template extension aliases. In a nutshell, this allows creating several potential layout paths for one LayoutDescriptor. Going back to the *.gohtml example, whenever we want to generate paths for the “html” suffix, apart from constructing paths ending with .html, we can also create those that end with .gohtml. Depending on which one is specified first, this method also defines the priority (i.e., whether to take *.html or *.gohtml first) at little to no extra cost. I created a branch on a Hugo fork to illustrate this particular example.

Exploring this idea further, it can give users a comfortable amount of control over the lookup order (but not too much control, still letting Hugo do its magic). These extension aliases can, of course, be configurable so that users can define and organize them in order of priority. For instance, we can have HTML templates with the *.ahtml extension take priority over *.bhtml, *.bhtml take priority over *.chtml, and so on.

Additionally, this allows users to keep their formatting, syntax highlighting, and other logic separate for each extension alias. Although it is a very long shot, this could potentially be a step towards modular template engines in Hugo, allowing users to choose their favorite template syntax and thus promoting increased adoption and community growth.

In summary, defining template extension aliases can be beneficial for both individual developers and the broader community. This feature aligns well with Hugo’s commitment to performance and adaptability, ensuring that it continues to meet the evolving needs of its users.

I want to thank you for your attention and say that I would be delighted to move this idea further and create a GitHub proposal if there is a suitable amount of interest. :slightly_smiling_face:

Happy coding!

I assume the case in mind is to support *.gohtml templates?

I’m not sure why you would want this, but in short, this is how the template extension vs lookup (should) work re extensions:

single.myoutputformat.html or single.html

So, the last example will match Media Type with extension html. I think there currently is an open issue about us currently only use the “first extension” in this lookup, but that could be fixed.

Would that solve your use case? You could then the gohtml extension to the HTML media type.

For reference: https://github.com/gohugoio/hugo/issues/10449

@bep Yeah, I noticed this “first extension” behaviour while looking through the code to figure out where to put that change I mentioned above. In the end I realised that this can be a little bit more than just allowing *.gohtml, allowing users specify aliases (and their priority) for any media type. If that kind of thing turned out to be of interest to the community, I could then formalise this with a proposal (as recommended in Hugo’s README). And yeah, maybe it can develop into further expanding Hugo’s idea of modularity (e.g. modular template engines as I mentioned above) :slight_smile: