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.67k stars 101 forks source link

Proposal: a `Context` API similar to `tera::Context` #159

Closed Folyd closed 1 year ago

Folyd commented 1 year ago

I'm investigating how to migrate from tera to minijinja. https://github.com/zineland/zine/issues/109 However, minijinja lacks a good Context API like the tera::Context which my project relied on heavily.

https://github.com/zineland/zine/blob/master/src/entity/mod.rs#L32-L40

mitsuhiko commented 1 year ago

The context itself is abstracted away in MiniJinja. On the input side anything that is Serialize can become the source of a context, within the template runtime model (eg: filters, functions and more) you can indirectly access the context via State.

In your case it sounds like you just need to define a type for yourself that implements Serialize that you can use (eg: a HashMap<String, Value> for instance.

Folyd commented 1 year ago

Thanks, @mitsuhiko. HashMap<String, Value> is fair enough, that's what the tera::Context did in the nutshell. It would be better if minijinja can provide an off-the-shelf Context API.

mitsuhiko commented 1 year ago

I'm not entirely sure what the point of this is. Can you outline how a context API helps? For what it's worth MiniJinja constructs just a BTreeMap when you use the context! macro for instance, but that's all hidden from a user as there does not seem to be much of a reason to expose it.

mitsuhiko commented 1 year ago

I will close this because I do not believe there to be value in adding this. There might be reasons for it, but so far I haven't been convinced that it would add anything.