Opened 8 years ago

Closed 5 years ago

Last modified 3 years ago

#9198 closed New feature (duplicate)

templatetag include adds new line

Reported by: Javier de la Rosa Owned by: nobody
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 (9)

comment:1 Changed 8 years ago by Malcolm Tredinnick

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 Changed 8 years ago by Javier de la Rosa

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 Changed 6 years ago by Luke Plant

Severity: Normal
Type: New feature

comment:4 Changed 6 years ago by anonymous

Easy pickings: unset

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

comment:5 in reply to:  4 Changed 6 years ago by Javier de la Rosa

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 Changed 5 years ago by Aymeric Augustin

Resolution: duplicate
Status: newclosed
UI/UX: unset

I believe this is a special case of #2594.

comment:7 Changed 4 years ago by anonymous

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

comment:8 Changed 3 years ago by 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.

comment:9 in reply to:  8 Changed 3 years ago by anonymous

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.

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