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

Eager evaluate syntax #243

Closed Folyd closed 1 year ago

Folyd commented 1 year ago

Lazy syntax evaluation could cause it hard to find the bugs, for example, trim_start_matches is not a built-in filter for minijinja, however, it didn't report an error until the if condition is met.

{% if site.cdn and image is startingwith("/static") -%}
    {% set image_url = site.cdn -%}
    {% set image = image | trim_start_matches("/static") -%}
{% else -%}
    {% set image_url = site.url -%}
{% endif -%}
mitsuhiko commented 1 year ago

I would be okay changing this to a compile time error as Jinja2 also forced the error (unknown filter or test) already at compile time.

mitsuhiko commented 1 year ago

I looked into this briefly and I think the changes necessary to eagerly detect this are probably not worth the effort. The code generator is intentionally infallible which leaves two places where this check could happen:

  1. it could be detected during parsing. This would require the parser to become aware of the environment
  2. it could be detected as a separate step after the templates were compiled

Either of those cases are abstraction wise quite far from the environments and multiple layers would have to be changed for that. This is particularly bad for the source abstraction which parses the templates and is itself held by the environment, but does not have access to the environment.

The best that could be done is run a validation step early before the template render, but that sounds like a quite expensive step.

I'm going to close this as WONTFIX given the cost and complexity associated with this.