Search code examples
gogo-html-template

What is the best way to call template.ParseFiles() dynamically in Go instead of passing in x number of strings?


I want to load a folder of HTML templates in Go and right now I can only pass in each file path as a string in an argument.

Example:

templates = template.Must(template.ParseFiles("../web/html_templates/edit.html", "../web/html_templates/view.html"))

Works fine.

This and similar solutions won't work:

templates = template.Must(template.ParseFiles("../web/html_templates/*"))

I'd like to specify my templates in a config file but I currently can't. What is the best way to go about this?


Solution

  • Use ParseGlob to parse a folder of HTML templates in one API call.

    templates = template.Must(template.ParseGlob("../web/html_templates/*.html"))
    

    See the Match function documentation for the glob syntax.