Opened 3 weeks ago

Closed 3 weeks ago

#36758 closed New feature (duplicate)

Template parser errors when using whitespace around pipe for certain filters

Reported by: Stephen Skett Owned by: Ahmed Asar
Component: Template system Version: 4.2
Severity: Normal Keywords: template, filter, pipe, whitespace, divisibleby, center
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Stephen Skett)

Based on discussion from previous support ticket #4746, I believe it should now be possible to add whitespace around the pipe-character when using a filter in a Django-template, if that is stylistically preferred. However, I experience 'TemplateSyntaxError' when I use whitespace around the '|', specifically the built-in divisibleby and center filters - as in the following examples.

With divisibleby in an if-tag (error: "Could not parse the remainder: '|' from '|'"):

{% for obj in some_collection %}
  {% if forloop.counter0 | divisibleby:num_items_per_row %}
    {% comment %}Some HTML here...{% endcomment %}
  {% endif %}
{% endfor %}

And, with center in a with-tag (error: "'with' expected at least one variable assignment"):

{% with '' | center:loop_limit_val as loop_range %}
  {% for _ in loop_range %}
    {% comment %}Some HTML here...{% endcomment %}
  {% endfor %}
{% endwith %}

I don't seem to encounter this error for most of the other built-in template-filters which I commonly use (e.g. date, floatformat, etc.), or with custom filters in my tag-libraries. Is this a bug with divisibleby / center, or am I missing something?

Change History (7)

comment:1 by Stephen Skett, 3 weeks ago

Description: modified (diff)
Keywords: center added
Summary: Template parserTemplate parser errors when using whitespace around pipe for certain filters

comment:2 by Ahmed Asar, 3 weeks ago

Hi Stephen,

Thanks for the detailed report. I was able to reproduce the issue with divisibleby and center when whitespace is added around the | — while other filters (like date) handle it fine. This does look like an inconsistency in the template parser.

Before proceeding, could you confirm which Django version you're using (exact version, e.g. 4.2.11 or main branch)? Also, would you be open to me investigating this further and potentially submitting a fix?

I’m happy to help, but I don’t want to step on your toes if you’d like to work on it yourself.

Best regards!

comment:3 by Stephen Skett, 3 weeks ago

Hi Ahmed, thanks so much for your swift response!
Yes, please feel free to look into this issue if/when you can -- I doubt I will find time to work on it in any kind of detail any time soon.
In answer to your question RE: specific Django version, I am using v4.2.4 for the project in question where I noticed this bug. I hope that helps.
Many thanks in advance,
Stephen

comment:4 by Ahmed Asar, 3 weeks ago

Thanks Stephen

comment:5 by Ahmed Asar, 3 weeks ago

Owner: set to Ahmed Asar
Status: newassigned

comment:6 by Ahmed Asar, 3 weeks ago

In which file is the problem located? I mean, which path?

Version 0, edited 3 weeks ago by Ahmed Asar (next)

comment:7 by Jacob Walls, 3 weeks ago

Resolution: duplicate
Status: assignedclosed
Type: UncategorizedNew feature

Duplicate of #29457

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