Opened 16 years ago

Closed 16 years ago

#7295 closed (fixed)

quotes, escaping and translation of string literals handled inconsistently in templates

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

Description

The Django template syntax sometimes accepts literal strings only in double quotes, whereas in other situations also single quotes work. Including a quote in a string by escaping it with a backslash doesn't work everywhere. Implementing SmileyChris's suggestion to #5756 (filter expressions as arguments for the cycle, ifchanged, ifequal, firstof and include tags) is difficult because of incosistencies in the underlying mechanisms.

String literals are dealt with in the FilterExpression and Variable classes in [source:django/trunk/django/template/__init__.py django/template/__init__.py]. The classes contain two different and overlapping approaches to unquoting string literals. FilterExpression embeds unquoting in the regex which parses filter expressions, and Variable takes a more "manual" approach.

With some modifications the following could be achieved:

  • both single and double quoted strings work everywhere
  • filters as template tag arguments easier to implement (see #5756)
  • escaping quotes works consistently
  • code re-use (DRY)

The challenge is to not lose performance.

Attachments (2)

7295.1.diff (10.6 KB ) - added by Antti Kaihola 16 years ago.
Patch for fixing issues with string literals in templates
7295.2.diff (11.2 KB ) - added by Antti Kaihola 16 years ago.
Patch now handles un-escaping correctly and optimizes numeric literals.

Download all attachments as: .zip

Change History (10)

by Antti Kaihola, 16 years ago

Attachment: 7295.1.diff added

Patch for fixing issues with string literals in templates

comment:1 by Antti Kaihola, 16 years ago

The template tag argument splitter seems to strip backslashes. This has to be addressed before this patch is valid.

by Antti Kaihola, 16 years ago

Attachment: 7295.2.diff added

Patch now handles un-escaping correctly and optimizes numeric literals.

comment:2 by Antti Kaihola, 16 years ago

The patch above delegates filter expression constant resolution to the Variable class to make sure that the same mechanism is used for both.

Resolving numeric constants is optimized compared to current trunk, since they are now resolved at template compile time. Trunk resolves them on every render.

django.utils.text.smart_split() is used only in template/__init__.py and it's hard to come up a use case outside template tag processing with its current behavior (see django-developers message). To make things simpler I moved the un-escaping behavior out from it to the unescape_string_literal() function.

As a result, variable resolution now supports quote escaping.

This patch passes all current tests, and makes it possible to implement #5756 as well.

comment:3 by Sung-jin Hong, 16 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:4 by Johannes Dollinger, 16 years ago

This would be fixed by the refactoring proposed in #7806.

But the smart_split() change would be useful anyway.

comment:5 by Michael Radziej, 16 years ago

milestone: post-1.0

comment:6 by anonymous, 16 years ago

Keywords: tplrf-fixed added

comment:7 by (none), 16 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:8 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

(In [10118]) Added consistent support for double- and single-quote delimiters in templates.

Some template filters and tags understood single-quoted arguments, others
didn't. This makes everything consistent. Based on a patch from akaihola.

Fixed #7295.

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