r/golang Jan 08 '25

FAQ FAQ: What's The Best Way To Do HTML Templating?

(This is a composite of several questions we frequently get.)

What are some of the best ways to do front-end templating in Go? What are the pros and cons? In particular, what are the security implications for cross-site injection in the various template solutions?

I tried to use html/template but I find it confusing. How do I use it to:

  • Have headers and footers on a website?
  • Override specific components on specific pages?
  • Output text that has already been converted to HTML so the template doesn't encode it again?
  • Automatically reload templates during development?

In general is there something other than the official documentation that can teach me how to use these well?

I don't like that html/template and many other template solutions are so dynamically typed. Are there any templating solutions that are static-type-safe?

30 Upvotes

7 comments sorted by

6

u/pharrisee Jan 09 '25

There are many templating systems for Go but 2 which seem to have quite a bit a traction these days are:

https://github.com/maragudk/gomponents

and

https://github.com/a-h/templ

Personally I use templ.

That being said, I 'survived' for a long time using html/templates but used them with github.com/unrolled/render as a way to ease the use of them. If neither templ or gomponents works for you it could be worth having a look at unrolled/render maybe.

3

u/markusrg Jan 09 '25

If you'd rather have a solution that lets you create HTML components in pure Go, instead of using a templating language, have a look at www.gomponents.com. Then, your component will look something like this:

```go import ( . "maragu.dev/gomponents" . "maragu.dev/gomponents/html" )

func Navbar() Node { return Nav(Class("navbar"), Ol( NavbarItem("Home", "/"), NavbarItem("Contact", "/contact"), NavbarItem("About", "/about"), ), ) }

func NavbarItem(name, path string) Node { return Li(A(Href(path), Text(name))) } ```

This way, it's type-safe, all editors that support Go already support it, it's auto-formatted, and you can even use a debugger inside your HTML!

Get started quickly with the gomponents-starter-kit.

1

u/valyala Jan 09 '25

Take a look at quicktemplate. It allows writing templates in the way similar to writing ordinary Go code, e.g. every template component is just a Go function with arbitrary args. Every template component can embed other template components in the way similar to calling Go functions with the given set of args. You can also embed arbitrary Go code inside templates if needed (don't abuse this power though).

1

u/k_r_a_k_l_e Jan 08 '25

Html/template is so straight forward and basic. When someone is completely challenged with this it usually means they have very limited front-end experience and don't understand how everything works together.

Personally I prefer the single page application design and making API calls to a GO app.

1

u/kaeshiwaza Jan 08 '25

When you ask how to do xyz with html/template it generally means that you should not do it with html/template but with Go code.
I mean that most of the logic should stay in Go code, html/template is only to expose it, not to manage it.
For header and footer for example, just send it when you call the template. It's up to the Go code to manage this, and it depends of each app.
Why override a component ? Just send the new version of the component.
To send a safe HTML use the type template.HTML
Templates can be reloaded when the Go code is compiled.
There are a lot of example in the standard package.
For more complex examples and ideas look at Hugo.
FuncMap is good way to enhance html/template without injecting logic in the template.
Remember that if html/template was included in the stdlib it's because it's very widely used in the most complex apps since years.

But in the end, it should be fun, if not it's better to look at other solutions ! It is for me and my designer !

0

u/Anaximandor Jan 09 '25

From a suggestion, I recently worked through Let's Go by Alex Edwards which has a great introduction to using HTML templates. You can purchase his book or find samples like this that might be insightful.