Code

Opened 7 years ago

Closed 5 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@…> 6 years ago.
5756.2.patch (6.2 KB) - added by akaihola 6 years ago.
Filters as arguments for cycle, firstof, ifchanged, ifequal and include. Depends on #7295.
5756.3.patch (435 bytes) - added by avsd 6 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 6 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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

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

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

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

  • Has patch set

comment:4 follow-ups: Changed 6 years ago by SmileyChris

  • Patch needs improvement set
  • Summary changed from Any template tag that uses resolve_variable instead of parser.compile_filter does not handle filters. to 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 6 years ago by SmileyChris

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

comment:6 in reply to: ↑ 4 Changed 6 years ago by akaihola

  • 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 6 years ago by akaihola

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

comment:7 Changed 6 years ago by akaihola

  • 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 6 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 6 years ago by emulbreh

  • 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 6 years ago by avsd

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

comment:10 Changed 5 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

(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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.