Patch #7521: Template if/ifequal/ifnotequal evaluate despite condition

97 views
Skip to first unread message

Joshua 'jag' Ginsberg

unread,
May 16, 2008, 2:40:55 PM5/16/08
to django-d...@googlegroups.com, ch...@moffitts.net
Howdy --

In python, conditionals only evaluate the appropriate branch and only
after evaluating the condition. We can say:

if 'foo' in bar_dict:
print bar_dict['foo']
else:
print "Foo not in bar"

Even if bar_dict doesn't have a key 'foo' the first branch of the
conditional is not evaluated, and so a KeyError is not raised. Every
language does this.

However Django's template system evaluates the branches prior to
determining which will be followed. The following template will raise
a TemplateSyntaxError:

{% ifequal 0 1 %}{% load non_existant_tag_library %}{% endifequal %}

... because non_existant_tag_library doesn't exist. This should not
raise a TemplateSyntaxError.

I've submitted a patch on ticket #7251 to correct this. Thanks!

-jag

Honza Král

unread,
May 16, 2008, 5:01:47 PM5/16/08
to django-d...@googlegroups.com
But python also PARSES both branches of the given expression, so if
there was a SyntaxError, it would fail as well, the code doesn't get
run, as well as the template don't get rendered.

there are two distinct phases to template evaluation - parsing and
rendering, some tags (load for example) have some logic in the parsing
phase, if that fails, the parsing fails which acts the same way in
python - it dies in flames

--
Honza Král
E-Mail: Honza...@gmail.com
ICQ#: 107471613
Phone: +420 606 678585

Joshua 'jag' Ginsberg

unread,
May 17, 2008, 6:23:13 PM5/17/08
to django-d...@googlegroups.com
Agreed. However, I don't believe loading a non-existent library should
be a syntax error any more than accessing a non-existent key in a
dictionary.

-jag

Joshua 'jag' Ginsberg

unread,
May 17, 2008, 6:25:25 PM5/17/08
to django-d...@googlegroups.com
And forgive my mistyping -- #7251, not #7521.

-jag

On Sat, May 17, 2008 at 6:23 PM, Joshua 'jag' Ginsberg

Honza Král

unread,
May 17, 2008, 6:47:17 PM5/17/08
to django-d...@googlegroups.com
On Sun, May 18, 2008 at 12:23 AM, Joshua 'jag' Ginsberg
<dja...@flowtheory.net> wrote:
>
> Agreed. However, I don't believe loading a non-existent library should
> be a syntax error any more than accessing a non-existent key in a
> dictionary.


but you need to load the library at parsing time to access the parsing
functions, there is no going around that... It might nit be ideal, but
I don't see any other way this could go with the current logic of
templates

Joshua 'jag' Ginsberg

unread,
May 19, 2008, 1:00:09 PM5/19/08
to django-d...@googlegroups.com
What about a {% loadif %} tag -- if this is an issue specific to
templatetag library loading, would that be more acceptable for
inclusion?

-jag

Reply all
Reply to author
Forward
0 new messages