ifequal support for AND OR

1,660 views
Skip to first unread message

ska...@gmail.com

unread,
Jan 12, 2006, 10:54:57 PM1/12/06
to Django developers
Hello,
In my recent project I have been finding myself wishing that the {%
ifequal %} tag had support for AND OR. I posted a <a
href="http://code.djangoproject.com/ticket/1209"/>patch to ticket
1209</a> that does just this. I was hoping to start a bit of
conversation here to see if others thought this might be useful.

The syntax is as follows:
{% ifequal fname "Jay" OR fname "James" %}
The name is Jay or James
{% else %}
The name is not Jay or James
{% endifequal %}

{% ifequal fname "Jay" AND lname "Johnson" %}
The name is Jay Johnson
{% else %}
The name is not Jay Johnson
{% endifequal %}

The patch includes tests to the tests/othertests/templates.py that has
convinced me (so far) that this does not break any existing use of {%
ifequal %}.

What do you think?

Tom Tobin

unread,
Jan 13, 2006, 12:14:36 AM1/13/06
to django-d...@googlegroups.com
On 1/12/06, ska...@gmail.com <ska...@gmail.com> wrote:
>
> Hello,
> In my recent project I have been finding myself wishing that the {%
> ifequal %} tag had support for AND OR.

Have you considered passing a boolean into the template from the view
with the corresponding value? I don't think the omission of complex
AND/OR logic was an oversight; I believe it's a deliberate attempt to
keep templates from becoming jammed full of business logic.

ska...@gmail.com

unread,
Jan 13, 2006, 1:06:28 AM1/13/06
to Django developers
I definitely understand why business logic should be kept out of the
template; I don't see why adding AND/OR support is any worse than
having if/if not/ifequal/ifnotequal/for.

During the Snakes & Rubies talk it was mentioned that many times there
has to be logic in the template, but just because it is logic does not
necessarily make it business logic.

The more dynamic a site becomes the more it helps to have better logic
available to you in the template. I have seen a few posts on this
board where people get tripped up trying to create navigation where the
page the user is on is highlighted in the navigation and so is the
section, or parent page that the current page belongs to. Yes, this
user could create there own custom tag but that is a job for the python
programmer and not the template designer. Plus a custom tag then
brings the possibility of putting html into the python code where it is
out of reach of the designer.

Russell Keith-Magee

unread,
Jan 13, 2006, 4:52:36 AM1/13/06
to django-d...@googlegroups.com
On 1/13/06, ska...@gmail.com <ska...@gmail.com> wrote:
>
> I definitely understand why business logic should be kept out of the
> template; I don't see why adding AND/OR support is any worse than
> having if/if not/ifequal/ifnotequal/for.
>
> During the Snakes & Rubies talk it was mentioned that many times there
> has to be logic in the template, but just because it is logic does not
> necessarily make it business logic.

This is true. The logic in Django templates is intentionally simple in
an attempt to enforce exactly this separation.

For example, the templates allow you to write a template that says (in
effect): if subscriber print X, else print Y. What does "subscriber"
mean in this context? That's a business logic decision, which should
be able to be changed independent of the way that a "subscriber" is
displayed.

If the templates allowed more complex logic, template designers might
be tempted to write a template that reads "if
last_payment.before(today - 1 year) and not admin". However, this
encodes a business logic decision in the template. Since the template
language does not allow complex queries, the designer cannot do this,
so they must establish their high level design requirements, and ask
the view developer to provide the appropriate context values - thereby
keeping business logic in the view, where it belongs.

Russ Magee %-)

hugo

unread,
Jan 13, 2006, 5:27:50 AM1/13/06
to Django developers
>If the templates allowed more complex logic, template designers might
>be tempted to write a template that reads "if
>last_payment.before(today - 1 year) and not admin". However, this
>encodes a business logic decision in the template. Since the template
>language does not allow complex queries, the designer cannot do this,
>so they must establish their high level design requirements, and ask
>the view developer to provide the appropriate context values - thereby
>keeping business logic in the view, where it belongs.

This is especially true if the templates are managed by the Web
designer. Designers are not necessarily programmers - for them a "{% if
subscribe %} ... {% else %} ... {% endif %}" is much easier to
understand than a more complex if expression that combines different
conditions with AND and OR (not to speak about the problem of operator
precedence, where you would have to define what operator has precedence
over the other and possibly have to provide bracket-grouping of
expressions - which all will make the parser for the if template tag
more complex).

For programmers, who need more complex templating, there allways is the
possibility to use Cheetah or something like that.

bye, Georg

Reply all
Reply to author
Forward
0 new messages