Opened 9 years ago

Closed 8 years ago

#5756 closed (fixed)

Any template tag that uses Variable instead of parser.compile_filter does not handle filters.

Reported by: sneeu Owned by: nobody
Component: Template system Version: master
Severity: Keywords: tplrf-fixed
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: UI/UX:

Description

On a page with the URL '/account/login/', I would expect the following to work:

{% ifequal request.path|slice:"9" "/account/" %}Do something for account pages.{% endifequal %}

As I understand it the underlying code (for ifequal and other template tag) uses resolve_variable so the first argument becomes:

request.get('path|slice:":9"')

Currently a workaround using the 'with' template tag is required:

{% with request.path|slice:"9" as path_prefix %}Do something ...{% endwith %}

Attachments (3)

5756.patch (1.9 KB) - added by Collin Grady <cgrady@…> 9 years ago.
5756.2.patch (6.2 KB) - added by Antti Kaihola 8 years ago.
Filters as arguments for cycle, firstof, ifchanged, ifequal and include. Depends on #7295.
5756.3.patch (435 bytes) - added by David Avsajanishvili 8 years ago.
A very little improvement of 5756.2.patch: changed line 182 in loader_tags.py (see #8088)

Download all attachments as: .zip

Change History (13)

comment:1 Changed 9 years ago by Chris Beaven

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

Changed 9 years ago by Collin Grady <cgrady@…>

Attachment: 5756.patch added

comment:2 Changed 9 years ago by Collin Grady <cgrady@…>

Only tag I saw that still did this was ifequal/ifnotequal - patch attached

comment:3 Changed 9 years ago by Collin Grady <cgrady@…>

Has patch: set

comment:4 Changed 9 years ago by Chris Beaven

Patch needs improvement: set
Summary: Any template tag that uses resolve_variable instead of parser.compile_filter does not handle filters.Any template tag that uses Variable instead of parser.compile_filter does not handle filters.

Checking source, we should probably fix this across the board (better to be consistent). Needs to be done for:

  • {% cycle %}
  • {% ifchanged %}
  • {% ifequal %}
  • {% firstof %}
  • {% include %} (in loader_tags)

comment:5 Changed 9 years ago by Chris Beaven

Keywords: easy-pickings added; ifequal variable filter template tag removed

comment:6 in reply to:  4 Changed 8 years ago by Antti Kaihola

Keywords: easy-pickings removed

Replying to SmileyChris:

  • {% cycle %}
  • {% ifchanged %}
  • {% ifequal %}
  • {% firstof %}
  • {% include %} (in loader_tags)

Enabling filter expressions as arguments to these tags turns out to be a bit complicated. Django's unit tests are full of single-quoted arguments to these tags, but compile_filter() (or actually FilterExpression) only supports double quotes. There are also issues with escaping quotes and translation.

These problems should be solved (#7295 has one possible patch) before going forward with this ticket.

Changed 8 years ago by Antti Kaihola

Attachment: 5756.2.patch added

Filters as arguments for cycle, firstof, ifchanged, ifequal and include. Depends on #7295.

comment:7 Changed 8 years ago by Antti Kaihola

Needs tests: set

The current patch for #7295 together with 5756.2.patch pass all current tests and make it possible to use filter expressions as arguments for the above mentioned five template tags.

Tests for the new feature should probably be added.

comment:8 in reply to:  4 Changed 8 years ago by anonymous

Replying to SmileyChris:

Checking source, we should probably fix this across the board (better to be consistent). Needs to be done for:

  • {% cycle %}
  • {% ifchanged %}
  • {% ifequal %}
  • {% firstof %}
  • {% include %} (in loader_tags)

It is probably too late in the game to bring this up, but should simple_tag handle this as well?

comment:9 Changed 8 years ago by Johannes Dollinger

Keywords: tplrf-fixed added

This would be fixed by the refactoring proposed in #7806 (which used akaihola's patch as a starting point, although not much is left).

Changed 8 years ago by David Avsajanishvili

Attachment: 5756.3.patch added

A very little improvement of 5756.2.patch: changed line 182 in loader_tags.py (see #8088)

comment:10 Changed 8 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [10119]) Fixed #5756, #6296 -- Most template tags can now handle filters in arguments.

Most of the hard work for this was done by akaihola.

There are still possibly some problems with the i18n template tags,
since they are written quite differently and a few other bug fixes have
to made before they can be fixed.

Note: See TracTickets for help on using tickets.
Back to Top