Opened 17 years ago

Closed 17 years ago

Last modified 17 years ago

#5149 closed (wontfix)

make template tag variables that can't be resolved pass as None

Reported by: Jack Moffitt <metajack@…> Owned by: nobody
Component: Template system Version: dev
Severity: Keywords: feature_request
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This is an enhancement request. In the same way that {{ variable }} will generate a blank string if variable does not exist, it would be nice if this happened also for template tag variables as well. For instance:

{% sometag list.0.name %}

would call the sometag inclusion tag with "" or None as the first variable if list is empty. This makes the template far simpler as it gets rid of:

{% if list %}

{% sometag list.0.name %}

{% else %}

{% sometag "" %}

{% endif %}

Right now the variable resolver generates an exception, which triggers the 500 logic or the traceback debug view since list has no 0 index. I'm sure there are other situations when this behavior would be desirable.

Change History (4)

comment:1 by Chris Beaven, 17 years ago

Triage Stage: UnreviewedDesign decision needed

comment:2 by Philippe Raoult, 17 years ago

Keywords: feature_request added

comment:3 by Jacob, 17 years ago

Resolution: wontfix
Status: newclosed

The correct way to handle this is by catching the VariableDoesNotExist exception in your template tag node and dealing with it there. There are times when you want to deal with invalid variables differently than empty ones, and silently suppressing that exception everywhere would make that impossible.

comment:4 by Jack Moffitt <metajack@…>, 17 years ago

I'm fairly sure I wouldn't have filed this if my template tag node even got executed. I believe that the template parser bails before my tag node is called, therefor there is no way for me to catch this exception, and instead I have to use the construction I posted in the template. I have tons of these everywhere. Perhaps this got fixed along the way since 0.96?

Note: See TracTickets for help on using tickets.
Back to Top