Skip to content

How to reference entire context as an object? #344

Answered by mitsuhiko
wez asked this question in Q&A
Discussion options

You must be logged in to vote

There is some risk with this behavior because it makes that you have a self-referential data structure if you are not careful. In pure theory it would be possible to write a globals() function (if an API was added to enumerate all variables in MiniJinja) that returns all variables, but even then you end up with the situation that actual globals are included that you probably don't want such as range(), debug() or that hypothetical globals() function.

What I would do instead is to say that you wrap the context that you pass for rendering with a helper that makes it self-referential within reason (eg: CONTEXT.name is possible, but CONTEXT.CONTEXT.name would fail). I added a simple example h…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by mitsuhiko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants