Keats / tera

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

Don't return an error if variable is missing in the context #930

Closed kakoc closed 1 month ago

kakoc commented 1 month ago

There are cases when I'd like to not trigger an error even if a variable is missing in the context. As I see based on a source there are no any ways how can I configure it right now(for now I can only use default pipe, but it means that I have to modify a template). Are there any ways how can I do that? Or probably you are ok to introduce some solution for that - I'm ok to be guided to contribute. Inside lookup_ident we have MAGICAL_DUMP_VAR. Probably alongside it we can introduce some additional check which will allow to fallback all missing template variables from the context, etc.

Keats commented 1 month ago

There won't be a mode for that no.

kakoc commented 1 month ago

There won't be a mode for that no.

Can you explain why pls, what are your thoughts about it? The use case is the following: I have some struct which will be converted into template context. but also there is a part which is user defined data(json), so I can't know its shape in advance, but that part also can be used inside the template. template is also user provided. Ideally I'd like to be able to specify some dictionary with fallbacks but even some soft mode in tera with which it doesn't report an error if a field is missing in the context would be awesome.

Keats commented 1 month ago

One of the main feature of Tera is that it does error with unknown variable rather than silently printing an empty string. Adding a mode to disable that would go against the core idea of the project.