How to provide an in-memory data (map/struct) as the datasource/context when rendering a template with Gomplate APIs in Go? #2001
-
Current Context: datasources/groups.yaml:
This is the short redacted snippet from .gomplate file:
Next Exploration:
If we want to provide an in-memory data (Go map/struct) as the datasource (context) similarly to what we're using in the Gomplate root template:
How can we achieve this similar task by providing a map as the datasource/context when rendering a template with Gomplate APIs in Go? Thanks a lot in advance for the support! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Thanks for the post @phong2tran, always nice to hear of projects where gomplate's being used! I see you're looking at using the v3 gomplate module, which is fine - that's the latest major release. However, be aware that there's also a v4 module which may be slightly easier to use (I'm thinking of the Renderer in particular). In particular, if you use the v4 module, you can provide There's a caveat too - v4 is not yet released, and there will be a few more breaking changes before it is. Thanks for this question though, because it highlights a particular difficulty in using gomplate programatically! |
Beta Was this translation helpful? Give feedback.
-
Thank you @hairyhenderson for the reply with suggestions on some alternative options! |
Beta Was this translation helpful? Give feedback.
Thanks for the post @phong2tran, always nice to hear of projects where gomplate's being used!
I see you're looking at using the v3 gomplate module, which is fine - that's the latest major release. However, be aware that there's also a v4 module which may be slightly easier to use (I'm thinking of the Renderer in particular).
In particular, if you use the v4 module, you can provide
NewRenderer
with anFSProvider
option, where you can supply an in-memory filesystem as provider. It's somewhat more awkward than simply usingtext/template
directly though, where you can provide arbitrary structs or maps directly as the context. Speaking of which, if you just want to render templates programatic…