> I would like to use if clause to check if a number is nagative or not
> with template in my html file. I read related part in Django template
> online document however I did not find what I need.
>
> Is it possilbe to do this with built-in Django filter functions? Or do
> I have to install a third-party template library?

You could either use the 'smarter {% if %} tag', which might be added to
Django 1.2 according to the "Features under consideration" list in the
Wiki. Grab it from http://www.djangosnippets.org/snippets/1350/

Or write a custom template filter. Since writing filters is so straight
forward and simple I recommend you to look into the docs:
http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters


There is no built-in filter for this in Django as it's a vital sign that
you're mixing template logic with view logic. I recommend you to ask
yourself the following question: "Does my template really have to figure
out x < y itself or shouldn't it just get this information from the view?".


If it's a common thing you might be better of writing a filter or
template tag specific to your use case.

I know it's hard to resist the urge "but I just want...". I can tell
from experience that investing that extra bit of time to do things the
right way pays off in the long run. Especially as you can later reuse
the code more easily.

Sometimes even simple stuff like {% ifequals foo 0 %} would fit better
into a custom filter. Just think of floating point values that are
almost zero. It's {% if foo|abs < 0.001 %} vs {% if foo|is_near_zero %}.
The latter provides you with the possibility to configure the threshold
in the config file while the former has the logic hardcoded in the template.


--mp

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to