Opened 16 years ago

Last modified 5 weeks 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 Malcolm Tredinnick, 16 years ago

Triage Stage: UnreviewedAccepted

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 like final_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.

comment:2 by Javier de la Rosa, 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 Luke Plant, 13 years ago

Severity: Normal
Type: New feature

comment:4 by anonymous, 13 years ago

Easy pickings: unset

versae's workaround doesnt seem to fix anything. The new lines seem to survive the trimming (!?).

in reply to:  4 comment:5 by Javier de la Rosa, 13 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 Aymeric Augustin, 12 years ago

Resolution: duplicate
Status: newclosed
UI/UX: unset

I believe this is a special case of #2594.

comment:7 by anonymous, 12 years ago

I found this solution, and it has worked for me:
https://github.com/twidi/django-include-strip-tag

comment:8 by david.wahlund@…, 10 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.

in reply to:  8 comment:9 by anonymous, 10 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:10 by Tim Graham <timograham@…>, 7 years ago

In 12cefee5:

Refs #15667 -- Prevented newlines in attrs.html widget rendering.

Removed the trailing newline from widget attrs.html template.
The solution may be revisited by fixing refs #9198 but not
for Django 1.11.

Thanks Dmitry Ivanchenko for the report and Preston Timmons for advice.

comment:11 by Tim Graham, 7 years ago

Resolution: duplicate
Status: closednew
Summary: templatetag include adds new lineMake the {% include %} strip a trailing newline
Type: New featureCleanup/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 Ahtisham Shafi, 5 weeks ago

Owner: changed from nobody to Ahtisham Shafi
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top