Introspecting template for required context variables
Hey there
I'm trying to use pongo2 for an email server with templating, and I'd like to be able to figure out what variables would be required to pass into the execution context in order to not leave any undefined. The purpose for this is pulling user data from a KV store, ideally I'd only pull the keys required by the template.
Any direction as to how I could go about this would be much appreciated, thanks!
Got similar situation here. I want to inspect what variables are there in the template. Didn't find any info about template meta APIs in documentation.
After some digging into source code, it seems Template.tokens would do the work.
// add to pongo2/templates.go
func (t *Template) GetTokens() *[]Token {
return t.tokens
}
Iterate throught tokens, find all Token.Typ == TokenIdentifier tokens, then extract Token.Val should do the trick (if situation is simple enough).
Would you consider adding some metadata related API ? Many thanks.
Just FYI, jinja2 has a meta API i used once in Python.
http://jinja.palletsprojects.com/en/2.10.x/api/#jinja2.meta.find_undeclared_variables