Skip to content

Is it possible to access a custom state object (eg. a Rust struct / reference) in a custom function? #338

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

You must be logged in to vote

Also to address your edit:

EDIT: in tera, the register_function function accepts a struct implementing the Function trait which of course allows you to store any number of fields in your custom struct...

You can also do that in MiniJinja. If you implement Object you can create a callable object. Just register it with env.add_global("my_function", Value::from_object(MyCallableStruct { ... })).

https://docs.rs/minijinja/latest/minijinja/value/trait.Object.html

Macros for instance in the engine are just custom objects that are callable: https://github.com/mitsuhiko/minijinja/blob/main/minijinja/src/vm/macro_object.rs

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@tesfabpel
Comment options

@mitsuhiko
Comment options

Answer selected by tesfabpel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants