Opened 6 years ago

Closed 3 years ago

Last modified 13 months ago

#9198 closed New feature (duplicate)

templatetag include adds new line

Reported by: versae 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 6 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 6 years ago by versae

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 4 years ago by lukeplant

  • Severity set to Normal
  • Type set to New feature

comment:4 follow-up: Changed 4 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 4 years ago by versae

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 3 years ago by aaugustin

  • Resolution set to duplicate
  • Status changed from new to closed
  • UI/UX unset

I believe this is a special case of #2594.

comment:7 Changed 3 years ago by anonymous

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

comment:8 follow-up: Changed 13 months 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 13 months 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