Opened 16 years ago
Last modified 9 months ago
#9198 assigned Cleanup/optimization
Make the {% include %} strip a trailing newline
Reported by: | Javier de la Rosa | Owned by: | Ahtisham Shafi |
---|---|---|---|
Component: | Template system | Version: | 1.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
If I have a template
# template1.html Line 1 Line 2
And I include this template in other one using templatetag include:
# template2.html {% include template1.html %} Line 3
The output must be:
# template2.html # template1.html Line 1 Line 2 Line 3
But the actual output is:
# template2.html # template1.html Line 1 Line 2 Line 3
This behavior is particularly annoying when it renders a template including JSON.
Change History (12)
comment:1 by , 16 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 16 years ago
As temporary workaround, I used a custom trim filter
def trim(value): return mark_safe(value).strip() register.filter('trim', trim)
Together with withtag templatetag.
comment:3 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
follow-up: 5 comment:4 by , 14 years ago
Easy pickings: | unset |
---|
versae's workaround doesnt seem to fix anything. The new lines seem to survive the trimming (!?).
comment:5 by , 14 years ago
Replying to anonymous:
versae's workaround doesnt seem to fix anything. The new lines seem to survive the trimming (!?).
You have to use the filter together to withtag templatetag. By example:
{% withtag include temaplate1.html as my_template %} {% my_template|trim %} {% endwithtab %}
But the code of withtag
templatetag is old, I don't know if it's working yet.
comment:6 by , 13 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
UI/UX: | unset |
I believe this is a special case of #2594.
comment:7 by , 12 years ago
I found this solution, and it has worked for me:
https://github.com/twidi/django-include-strip-tag
follow-up: 9 comment:8 by , 11 years ago
Using Django 1.6 I'm including a template inside of a <div>, this adds a newline to the html that moves the whole content down a line. The newline is impossible to remove, even with the spaceless tag. This needs to be fixed somehow. Case #2594 deals with more specific cases of white space handling. This is only related to the new line from include and why it's not cleared using spaceless.
comment:9 by , 11 years ago
Replying to david.wahlund@…:
Using Django 1.6 I'm including a template inside of a <div>, this adds a newline to the html that moves the whole content down a line. The newline is impossible to remove, even with the spaceless tag. This needs to be fixed somehow. Case #2594 deals with more specific cases of white space handling. This is only related to the new line from include and why it's not cleared using spaceless.
Nevermind. There was a newline, but the space was an invisible character in the file.
comment:11 by , 8 years ago
Resolution: | duplicate |
---|---|
Status: | closed → new |
Summary: | templatetag include adds new line → Make the {% include %} strip a trailing newline |
Type: | New feature → Cleanup/optimization |
We might reconsider changing this as discussed on django-developers. The implementation with respect to backwards-compatibility is still to be decided.
comment:12 by , 9 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
For backwards compatibility reasons we cannot change the default behaviour now (there will be templates that rely on the newline being inserted). It might be possible to add a "trim trailing newline" option to the
include
tag, though, if it can be given a very short name. Something likefinal_newline=0
but with a way better name than that (I'm giving it an "=0" bit so that we can tell the different between the controlling parameter and the name of any file to be included.