Hacker News new | ask | show | jobs
by smarkov 750 days ago
I've been using Go's html/template for the last couple of months and all I can say is that it's still very much a toy. It's fine for basic loops and conditionals but anything beyond that is extremely limited and having proper reusable components is a constant fight against limitations, specifically because of the lack of being able to pass multiple arguments to a template. I wish the Go team had gone with something more akin to PugJS[1], which in contrast is very rich in features, flexible and dare I say fun to build components and pages with.

[1] https://pugjs.org/api/getting-started.html

1 comments

> because of the lack of being able to pass multiple arguments to a template

I'm not sure if this I understand the issue correctly, but one can pass multiple arguments, by wrapping them e.g. into a hash (https://go.dev/play/p/89gP42K8XRb):

  package main
  
  import (
      "log"
      "os"
      "text/template"
  )
  
  func main() {
      err := template.Must(
          template.New("").Parse("{{ .greeting }}, {{ .user.name }}!"),
      ).Execute(os.Stdout, map[string]any{
          "user": map[string]any{
              "name": "earthling",
          },
          "greeting": "hello",
      })
      if err != nil {
          log.Fatal(err)
      }
  }

In my experience, the module is far from a toy, but it's not always obvious how to use it properly, at first.