Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Tornado Templates: Consume newline that follows {% %} tags #291

Closed
christarnowski opened this Issue · 16 comments

9 participants

@christarnowski

The output of Tornado's template processor suffers from excessive newlines for templates with large number of {% %} tags, which are usually placed on separate lines for better readability.

One of the possible solutions to this issue is to instruct the parser to consume a newline character that follows %} tag.

@patrickod

Is there any update on fixing this issue? It's been over half a year since this issue was raised and it's still present in 2.1.1.

@kromped

I'm having the same issue as well. Any update on this?

@twarkie

What about a compact mode or something that doesn't do newline at all? I'd use it!

@amishurov

any update?

@bdarnell
Owner

No, there is no update on this request. I'm generally of the opinion that it's better for the text outside the braces to be rendered exactly as-is rather than introduce special cases to eliminate newlines. What are you doing where you want to get rid of these newlines? In html newlines don't matter, and when newlines do matter you want to be able to ensure that they do appear where you expect them just as much as you want to get rid of them when you don't want them.

@amishurov

I have log records (like objects with some attributes: level, time, etc) that I insert into div block via websocket. Before write message to socket, I make template rendering:
{% if level == 'INFO' %}<\b style="color:white;">{{ message }}<\/b>
{% elif level == 'ERROR' %}<\b style="color:red;">{{ message }}<\/b>
{% else %} {{ message }}
{% end %}

If I format this block with new lines a also have new lines in my html.

@bdarnell
Owner

Why do you care about newlines in your html? Is this in a <pre> block?

The proposed change wouldn't help your example, since the newline does not immediately follow the %} (except for the last line).

@amishurov

In my example there is no newlines, becuase I use such "bad" formatting, that logical expressions and text on one line.
It is inside div. My mistake that I use "white-space: pre;" style, probably

@bdarnell
Owner

So in this case you don't want the newline that follows the "if", but you do want the newline that precedes the "else".

We already do some whitespace manipulation to squash repeated spaces (while preserving newlines). It would be reasonable to change this to not treat newlines and special and just replace any whitespace with a single space, although this would not actually help people trying to use preformatted whitespace.

I still feel that if you want preformatted whitespace, you'd be better off if you could turn off the whitespace magic we currently do instead of adding more.

@bdarnell bdarnell added the template label
@Paranaix

Even if this issue is fairly old I stumpled about this problem just some days ago while creating a web application with tornado. Whitespaces matter in my humble opinion. With the current implementation the generated output is pain in the ass to debug if you pay a little attention to formatting inside your templates. Due do this I decided to implement that particular section as a UIModule even if it was fairly simple/small and could have been done directly as template (well to be honest, the overall bad readability of templates containing more complex statements was a reason too).

@gagabla

Stubled uppon this too, in jinja2 one can use minus signs as addition to {% and %} to indicate, that leading/trailing whitespace (including newlines) should be removed. This allows both readable templates and condensed whitespace in the generated file.

@animalize

Yes, Jinja2's way is good, please consider it.

@bdarnell
Owner

If you're willing to mark up your template to control whitespace on a case-by-case basis, we already have that: just put a comment directive {# before the whitespace you want to remove and #} after. But I really don't like any solution based on manual markup; it seems very labor-intensive and whitespace in HTML just isn't that important. Why is a little extra whitespace such a "pain to debug"?

@animalize

Thanks your reply, it works, but comment directives make the template a bit unreadable.

IMO, some parts of HTML are sparse, while other parts of HTML are dense, it's beautiful, although it's meaningless for HTML parser.

But in speical cases it's useful, for example using template engine to generate latex/markdown/pure-text/others document.

I have a feel, Jinja2's -sign is good for HTML, but not flexible enough for other documents.

@bdarnell bdarnell closed this issue from a commit
@bdarnell bdarnell Introduce `oneline` template whitespace mode.
This slightly-more-aggressive whitespace removal doesn't actually
save any space, but it removes the very conspicuous blank lines
that are often left around template directives.

Closes #291.
Closes #1083.
254783a
@bdarnell bdarnell closed this in 254783a
@bdarnell
Owner

I've introduced some new options for whitespace control. The template_whitespace="oneline" mode is like the current collapse_whitespace mode except that it replaces a run of whitespace including a newline with a single space, instead of preserving newlines. This is less flexible than jinja2's options but I don't believe the complexity of options like those is justified.

@animalize

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.