Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

cannot set compress_whitespace's value for Template #178

Open
jeffreyhsu opened this Issue · 11 comments

5 participants

@jeffreyhsu

tornado template will trim all spaces. but i want to use

 to format my code.
i found there's a parameter to configure this 'compress_whitespace', but there's no way to set this param. #bug

@bdarnell
Owner

How would you want to configure this? Would you want a global application setting or something more narrowly targeted? We already try to disable compress_whitespace when the pre tag is in the same _Text node as the code, but the template engine isn't smart enough to keep track of whether or not it's inside a pre tag that was started elsewhere.

Note that compress_whitespace only applies to text that's literally a part of the template - data that's included via {{ }} blocks doesn't get compressed.

@jeffreyhsu

I want to format the code with 'pre' tag. but 'pre' also compress-whitespace.
I wish there're both global application setting and narrowly setting such like self.render('xxx', compress_whitespace=False).
but anyway, i think the 'pre' tag shouldn't compress-whitespace.

@nickbaum

I believe I'm running into this as well. I'm trying to do something like this:

<pre>&lt;script&gt;{% include "script.js" %}&lt;/script&gt;</pre>

... where the white space would not be compressed for script.js.

What's the best way to achieve this?

@bdarnell
Owner

To be clear, script.js is not actually javascript, it's a tornado template to be processed on the server, right? An easy hack would be to name the file something other than .js, since whitespace is only compressed in templates named .html and .js.

@nickbaum

A quick follow up on this. Simply renaming the file didn't work, as it seems {% include %} made it inherit the whitespace collapsing.

However, renaming the file and using render_string in the handler worked, at which point I could just inject the string in the template.

Thanks for the suggestion, Ben!

@bdarnell
Owner

You may want to consider {% module Template("foo") %} instead of {% include "foo" %} - the former offers more isolation between the template files so I think it would do what you want here.

@nickbaum

Ah yes, that worked as well. Thanks for the tip!

@aragaer

How can I enable compress_whitespace for xhtml?

@bdarnell
Owner

You mean for the file extension .xhtml? Currently the only way to set compress_whitespace (apart from the default behavior that sets it to true for .html and .js, and false for all other extensions) is to subclass tornado.template.BaseLoader or Loader and override _create_template.

@zhangxiansheng

bdarnell:We already try to disable compress_whitespace when the pre tag is in the same _Text node as the code, but the template engine isn't smart enough to keep track of whether or not it's inside a pre tag that was started elsewhere.
jeffreyhsu:but anyway, i think the 'pre' tag shouldn't compress-whitespace.

Yes,
"compress_whitespace" is always about we use "<pre>".
When I use google-code-prettify, I find it is always wrong, At last I find it's the wrong with tornado's "compress_whitespace". But it is not wrong but just a small bug.
Because it already can disable compress_whitespace the <pre>, but the bug is that it always doesn't work with <pre id="XXX"> or <pre class="XXXX> ... <pre> can only be <pre>. So now I have to use js to do work on the <pre> when document is onloaded. Out of the <pre>to put a span tag like <span class="pre-python"> ; then $(".pre-python pre:first") .....

It works, but it is inconvenient. Can this thing of tornado be mended with some small change via regular expressions

@bdarnell
Owner

I don't expect tornado's templates will ever get smart enough to do more sophisticated pre tag detection. I think the right answer is that if you're using pre tags in your templates themselves (as opposed to data that is inserted into the templates at runtime), you should just turn off whitespace manipulation entirely. We don't have a good way to do this, but it has been a very low priority because there's an easy workaround: just name your templates something like .tmpl instead of .html and then the whitespace will be left alone.

@bdarnell bdarnell added the template label
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.