tags:

views:

134

answers:

5

I'm trying to do this, and failing. Is there any reason why it wouldn't work in Django template syntax? I'm using the latest version of Django.

{% ifequal entry.created_at|timesince "0 minutes" %}
A: 

I think you can, though I don't see any uses of it my code base. Maybe entry.created_at|timesince isn't producing the value you expect. Try putting

X{{entry.created_at|timesince}}X

into your template and seeing what value it produces. The X's are so you can see leading or trailing space, in case that is the problem.

Ned Batchelder
No spaces; the string in the comparison is identical to the output of the filter. Hrm.
rmh
Ugh, this is the thing I don't like about the Django template system: a seemingly arbitrary distinction between what you're allowed to do, and what you're not allowed to do. It's an imperfect ad-hoc compromise, disguised as a separation of powers...
Ned Batchelder
"seemingly arbitrary" -- until you climb inside the head of the authors; then it makes a lot of sense. Remember, the template is parsed with simple regexes and interpreted by a simple interpreter.
S.Lott
That's what I mean by an ad-hoc compromise: it was designed to simplify its construction, rather than to aim for some other goal. Why can I filter for display but not comparison? Why can I compare, but not against filtered data? There's no clear philosphical distinction there.
Ned Batchelder
+5  A: 

It doesn't work because it's not supposed to work. What you're asking for is not part of the template language.

You can't apply a filter in the middle of tag like {% ifequal. When a template tag uses a variable, it doesn't expect an expression, it expects a variable, nothing more.

That kind of logic -- extracting time since, comparing, etc. -- is what you're supposed to do in your view function.

Your view function then puts a "zerominutes" item in the context for the template to use. Templates just can't do much processing.

They're designed to do the minimum required to render HTML. Everything else needs to be in your view function.

S.Lott
It doesn't cause an exception though; it just fails silently. Is that normal? Template syntax errors should raise exceptions.
rmh
Yes, it is. When the template engine can't resolve stuff, it tries to ignores it. In this case the overall syntax was largely correct. The attempt to apply a filter looks right syntactically, but produces no output.
S.Lott
@S.Lott I don't think this has been design decision but a neglect. In some built-in tags, filters do work, and it causes confusion that in others they don't (there's a similar related situation with single and double quotes). For more information, see links to tickets in my answer.
akaihola
+5  A: 

{% ifequal %} tag doesn't support filter expressions as arguments. It treats whole entry.created_at|timesince as identifier of the variable.

Quik workaround: introduce intermediate variable with expresion result using {% with %} like this:

{% with entry.created_at|timesince as delta %}
    {% ifequal delta "0 minutes" %}
    ....
    {% endifequal %}
{% endwith %}
Alex Koshelev
A: 

I finally gave up on using the Django template language for anything other than the simplest pages. Check out Jinja2 for an almost-syntax-compatible alternative. And yes, you can choose which to use on a page-by-page basis.

Peter Rowell
+2  A: 

See ticket #5756 and links in its comments for more information. A patch for Django in ticket #7295 implements this functionality. A broader refactoring of the template system based on #7295 is proposed in ticket #7806, and it would fix this issue among others.

I don't think making such comparisons to work would be against the design philosophy of Django templates.

akaihola