Keats / tera

A template engine for Rust based on Jinja2/Django
http://keats.github.io/tera/
MIT License
3.5k stars 281 forks source link

Add a "per_render" Context option #866

Closed harrisonkaiser closed 11 months ago

harrisonkaiser commented 11 months ago

In the case you have a particularly large Context that you can't easily clone, it is useful to have a the ablity to augment the Context on a particular render. By adding an extra context that can be specified on each render call the large Context can be shared with other render calls across multiple threads, thus sidestepping the need to clone and modify the Context for each thread.

I, somewhat aribrary, choose the per_render_context to be a fallback context, if the value is not found in the Context then look in the per_render_context. It might be desirable to check for overlapping keys and error out if they exist.

Keats commented 11 months ago

Thanks, I won't add new features to the v1 though. v2 progess is at https://github.com/Keats/tera2 but kinda stalled for a bit due to parental leave.

harrisonkaiser commented 11 months ago

Thanks, I won't add new features to the v1 though. v2 progess is at https://github.com/Keats/tera2 but kinda stalled for a bit due to parental leave.

Thanks for the quick response! Tera is a great tool and v2 looks exciting!

Keats commented 11 months ago

Note that in v2, cloning contexts should be cheaper so maybe that specific feature won't be needed?

harrisonkaiser commented 11 months ago

Note that in v2, cloning contexts should be cheaper so maybe that specific feature won't be needed?

Will it be cheap to modify the cloned context? I'd like context to vary slightly on each render.

One option is to use: https://docs.rs/im/latest/im/

Keats commented 11 months ago

Will it be cheap to modify the cloned context?

Not really. Most of the Value will be in Arc so cloning is cheap but modifying it, not so much. You can play around https://github.com/Keats/tera2 to see how it behaves. It's mostly working, except for functions/filters/tests not implemented yet and error reporting not working for rendering yet.

For the context/serialization part, it should be kinda complete.

harrisonkaiser commented 11 months ago

Okay, I'll close this PR.