cannot set compress_whitespace's value for Template #178

Closed
jeffreyhsu opened this Issue Nov 23, 2010 · 12 comments

Comments

Projects
None yet
5 participants

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

Owner

bdarnell commented Nov 29, 2010

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.

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.

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?

Owner

bdarnell commented Apr 11, 2012

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.

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!

Owner

bdarnell commented Apr 19, 2012

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.

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

aragaer commented Oct 17, 2013

How can I enable compress_whitespace for xhtml?

Owner

bdarnell commented Oct 20, 2013

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.

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

Owner

bdarnell commented Apr 15, 2014

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 added the template label Jul 16, 2014

bdarnell closed this in 72251a2 Jul 5, 2015

@bdarnell bdarnell added a commit that referenced this issue Jul 5, 2015

@bdarnell bdarnell Add `{% whitespace %}` template directive.
This allows per-template and even per-section changes to whitespace
handling.

Closes #178.
d8d5f58
Owner

bdarnell commented Jul 5, 2015

I've introduced several new ways to control whitespace handling: the template_whitespace Application setting, the whitespace argument to the template Loader constructor, and the {% whitespace *mode* %} directive inside a template itself.

Available modes are single (collapse repeated whitespace to a single character, while preserving newlines. This is currently the default for files named .html or .js), all (preserve all whitespace as found in the template source, currently the default for all other files), and oneline (like single but replace newlines with spaces instead of preserving them).

In addition, the {% include %} directive no longer causes the included template to inherit its parent's whitespace mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment