Skip to content

Add ergonomic control over behaviour of missing variables in templates #5

Open
@LilyAcorn

Description

@LilyAcorn

Code of Conduct

  • 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.

Implementation Suggestions

Instead of:

{{ name }}
{{ biography }}

we could allow:

{{ name! }}
{{ biography? }}

and name would be required whereas biography would be optional.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Idea refinement - Team creation - For approval

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions