mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.48k stars 634 forks source link

[Feature Request] Extract required context variables from template string #1444

Open alex-statsig opened 12 months ago

alex-statsig commented 12 months ago

I'm building an internal tool to help teams quickly plug variables into an arbitrary nunjucks template and render it (we're using it to generate SQL queries). It would be nice if there was a way to extract the required variables (either through all codepaths are at least through the executed path of a given "render" call) so we can provide them with a list of variables to fill in. I have a rough heuristic with some regex (templateStr.matchAll(/{{\s*([a-zA-Z0-9_]+)\s*}}/g))) but it misses anything other than simple context references. I tried using a proxy object into renderString() but it seems because of the way its loaded (via first storing into a map rather than accessing as needed) this doesn't help.

NextThread commented 10 months ago

hey, can I work on this issue