django / django/new-features

Add ergonomic control over behaviour of missing variables in templates

Open
#5 24 comments 49 reactions 0 assignees View on GitHub
Templates
Dominant language
No language data
Stars
188
Forks
7
PR merge metrics
No merged PRs in 30d

Description

### Code of Conduct

- [x] I agree to follow Django's Code of Conduct

### Feature Description

Introduce `!` as an explicit marker that a template variable is required to exist in the `context`.
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:

* Pages still render instead of returning a `500` error when a minor programming error is made.
* Template authors can deliberately design a section to be omitted when variables are not in the `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](https://code.djangoproject.com/ticket/28618).

### Implementation Suggestions

Instead of:

```
{{ name }}
{{ biography }}
```

we could allow:

```django
{{ name! }}
{{ biography? }}
```

and `name` would be required whereas `biography` would be optional.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.