-
Notifications
You must be signed in to change notification settings - Fork 1
Add ergonomic control over behaviour of missing variables in templates #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thank you LilyFoote for sharing your idea! We have a lot of them so please be patient. You can see the current queue here. If you'd like to learn about other ways to get this idea more attention, please see this page. Community instructionsFor commenters, please use the emoji reactions on the issue to express support, and/or concern easily. Please use the comments to ask questions or contribute knowledge about the idea. It is unhelpful to post comments of "I'd love this" or "What's the state of this?" Reaction Guide
|
How do you envision this working with template inheritance? |
I'm not sure this would need to do anything special with template inheritance. Do you have any specific examples in mind where you think there might be a problem? |
I don't know if it'll be a problem. I'm thinking a parent template from another library has a variable as required and the developer isn't aware of it. Adding a required variable to templates that are already in use would probably be a backwards incompatible change. |
Right! I think that would be a decision for projects defining those templates to make on how they want to handle the change. In Django itself, I think we'd be pretty cautious about it. |
What would happen with filters applied to optional variables? I'm not even sure what happens now when a variable is undefined - does the empty string get passed down to the filters? I see an opportunity here to maybe short circuit filter evaluation if the variable is undefined, similar to the JS I'm a little bit concerned about readability though, especially the exclamation mark being quite similar to the pipe symbol. |
The current behaviour is: an undefined variable becomes an empty string, unless For the new behaviour, I think we'd want the undefined variable to become
Potentially, yes.
Yeah, this is something I'm concerned about too, but I'm not sure the alternative of a named filter ( |
Is it even possible to use this in django the same release it comes out? The templates we'd use it in would have to be ones we're confident aren't being inherited from. |
Uh oh!
There was an error while loading. Please reload this page.
Code of Conduct
Feature Description
Introduce
!
as an explicit marker that a template variable is required to exist in thecontext
.Introduce
?
as an explicit marker that a template variable is optional. (Django's default behaviour.)Problem
Templates silently convert missing variables to an empty string. This behaviour is useful in many situations:
500
error when a minor programming error is made.context
.However, this implicit behaviour is inconvenient for people who want to get an error during development when a variable that should always be present is omitted by mistake. By adding explicit syntax for required and optional variables, we give developers and teams extra control to opt-in to the desired behaviour where appropriate.
Request or proposal
proposal
Additional Details
This has been discussed on the forum: https://forum.djangoproject.com/t/raise-error-for-missing-variable-used-in-template/39776
This could also resolve this accepted ticket.
Implementation Suggestions
Instead of:
we could allow:
and
name
would be required whereasbiography
would be optional.The text was updated successfully, but these errors were encountered: