Opened 11 years ago

Closed 10 years ago

Last modified 10 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: master
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: UI/UX:


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 %}

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 %}

{% 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 Changed 11 years ago by Chris Beaven

Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 11 years ago by Philippe Raoult

Keywords: feature_request added

comment:3 Changed 10 years ago by Jacob

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 Changed 10 years ago by Jack Moffitt <metajack@…>

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