Re: [tornado] some blank line in template.

421 views
Skip to first unread message

Russ Weeks

unread,
Jul 26, 2012, 12:50:23 PM7/26/12
to python-...@googlegroups.com
Hi, Simon,

Does your template file end with ".html" or ".js"?  If so, I think Tornado will compress whitespace automatically.

-Russ

On Thu, Jul 26, 2012 at 8:50 AM, simon <hap...@gmail.com> wrote:
The tornado's template make many blank line ,especially in the iteration.How to do better.

simon

unread,
Jul 26, 2012, 1:19:23 PM7/26/12
to python-...@googlegroups.com, rwe...@newbrightidea.com
Hi Russ
My template file end is with ".html".
I can't delete all end of line in the code.
Don't you have this problem?
This tornado's version is 2.1.
 
simon

simon

unread,
Jul 26, 2012, 2:05:34 PM7/26/12
to python-...@googlegroups.com
Hi,guys 
 
I have a test.this prove my problem.
 
this code:
//===========================================
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("test.html")
application=tornado.web.Application([
    (r"/",MainHandler),
])
if __name__ == "__main__":
    application.listen(8000)
    tornado.ioloop.IOLoop.instance().start()
//===========================================
 
the html file "test.html"
//===========================================
<html>
<head>
</head>
<body>
test1
test2
{%for i in range(0,10)%}
{%if i%2==0 %}
<br>test
{%end%}
{%end%}
</body>
</html>
//===========================================
 
 
the Result:
//============================================
<html>
<head>
</head>
<body>
test1
test2


<br>test





<br>test





<br>test





<br>test





<br>test




</body>

</html>

//=============================================

Lorenzo Bolla

unread,
Jul 27, 2012, 5:45:30 AM7/27/12
to python-...@googlegroups.com
This is a feature! ;-)

The fact is that the template syntax, although it resemble Python's, does not actually rely on newlines and indentation to define blocks. Therefore, any newline at the end of, let's say, {% for... %} or {% if %} is still considered a valid character and printed in the result.

To achieve what you want (but why do you want it, anyway?), you can do this:

<html>                                                                             
<head>                                                                             
</head>                                                                            
<body>                                                                             
test1                                                                              
test2{%for i in range(0,10)%}{%if i%2==0 %}                                        
<br>test{%end%}{%end%}                                                             
</body>                                                                            
</html>

L.

ushuz

unread,
Sep 14, 2013, 3:58:25 AM9/14/13
to python-...@googlegroups.com
Could tornado offer an option about this? Like `lstrip_blocks` or `trim_blocks` in Jinja2.
Change my templates to eliminate empty lines will make them look creepy.
Readability counts, right?

Ben Darnell

unread,
Sep 14, 2013, 11:54:07 AM9/14/13
to Tornado Mailing List
On Sat, Sep 14, 2013 at 3:58 AM, ushuz <hszq...@gmail.com> wrote:
Could tornado offer an option about this? Like `lstrip_blocks` or `trim_blocks` in Jinja2.
Change my templates to eliminate empty lines will make them look creepy.
Readability counts, right?

Readability of the templates definitely counts, but why do you care about empty lines in the generated output?  My feeling is that when whitespace actually matters (e.g. a <pre> block, not just lots of unsightly empty space) it's better to keep the rules simple and let the templates be a little ugly than to have more complicated rules about which newlines pass through and which ones don't.  See the discussion in 

-Ben
 

--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages