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)
Change History (10)
by , 16 years ago
Attachment: | 7295.1.diff added |
---|
comment:1 by , 16 years ago
The template tag argument splitter seems to strip backslashes. This has to be addressed before this patch is valid.
by , 16 years ago
Attachment: | 7295.2.diff added |
---|
Patch now handles un-escaping correctly and optimizes numeric literals.
comment:2 by , 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 , 16 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:4 by , 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 , 16 years ago
milestone: | → post-1.0 |
---|
comment:6 by , 16 years ago
Keywords: | tplrf-fixed added |
---|
comment:8 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Patch for fixing issues with string literals in templates