Keats / tera

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

Allow only pulling in specific builtins #890

Closed BD103 closed 8 months ago

BD103 commented 8 months ago

In order to use builtin filters and functions, you have to enable the builtin feature:

https://github.com/Keats/tera/blob/290889e61e9fda317f42f284e7a875342424d646/Cargo.toml#L48

This feature requires 6 different dependencies, some of which you may not need (like rand). Instead of requiring an "all or nothing" approach, what if instead specific builtins are enabled only if the dependency they require is also enabled?

For instance, slugify would only require the slug feature to be enabled:

/// Transform a string into a slug
- #[cfg(feature = "builtins")]
+ #[cfg(feature = "slug")]
pub fn slugify(value: &Value, _: &HashMap<String, Value>) -> Result<Value> {
    let s = try_get_value!("slugify", "value", String, value);
    Ok(to_value(slug::slugify(s)).unwrap())
}

Source

I would love to work on this if the idea gets approved! :)

Keats commented 8 months ago

The way it's going to work for v2 is that you will have some built-in filters/fn/tests that don't require deps directly in Tera. Anything else will be a in a tera-contrib package with individual features for each element

BD103 commented 8 months ago

Would I be able to make a PR for v1 while v2 is still a work in progress? Or do you think v2 is stable enough to make the switch already?

My plan is to decrease the amount of dependencies Bevy uses in its CI, with Tera being the first on the list. The only filter it needs is slugify. The templates are relatively simple.

Keats commented 8 months ago

v2 is not coming that soon. For your usecase you can just disable the built-in feature and copy paste the slugify filter?

BD103 commented 8 months ago

To be honest I didn't realize you can register your own filters. Thanks for the idea!

Have a good weekend