mitsuhiko / minijinja

MiniJinja is a powerful but minimal dependency template engine for Rust compatible with Jinja/Jinja2
https://docs.rs/minijinja/
Apache License 2.0
1.63k stars 95 forks source link

async support for Object trait #510

Closed gemhtcr closed 5 months ago

gemhtcr commented 5 months ago

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

Is it okay to add a async version for call/call_method in Object trait ?

I understood that it might be difficult to add it because of the lifetime issue, but there are some cases where we have to query from database inside call/call_method to proceed

Proposal

pub trait Object: + Send + Sync  {
     [...]
     fn call(...) {...}
     async fn call_async(...) {... /*so we can query from database on the fly*/}    //  proposal

     fn call_method(...)  {...}
     async fn call_method_async(...)  {... /*so we can query from database on the fly*/}  // proposal
     [...]
}

impl minijinja::Template {
    [...]
    fn render() {...}
    async fn render_async() {...}  // proposal
    [...]
}

By supporting async above, it'd be more extensive and natural for minijinja to interact with database

Possible workaround

A possible workaround is to use undeclared_variables to query before calling render but that required extra complexity to parse those variables

mitsuhiko commented 5 months ago

Having async methods would mean that the entire render code path would have to become async. I don't think that's particularly appropriate for how the engine works. What I would do is just to call runtime.block_on in the method call. There are various patterns that could be done around it to make this better/more efficient if needed.