Using gin with pongo2/v4 and embedded templates

You’d like to use pongo2/v4 with gin and embed templates with go:embed.

I’m using cobra for my cli parsing and commands.
So

edit cmd/ui.go

ui/templates.go

ui/templates/base.html.twig

ui/templates/layout.html.twig

ui/templates/index.html.twig

and finally run

to download packages

What have we done?

  • In ui/templates.go we said that the directory templates, which is a subdirectory of ui/ should be embedded as the exported variable Templates
  • In line 36 of cmd/ui.go we imported that variable and stripped it of its templates/ prefix
  • line 41 we turned that into a HttpFileSystem and passed that to pongo2’s FileSystemLoader
  • line 42 then creates a renderer that is compatible with pongo2/v4
  • line 43 we assign that renderer to be gin’s HTML renderer

Any questions -> leave a comment

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.