Include tag doesn't preserve indentation level on included text

65 views
Skip to first unread message

Marko Seppälä

unread,
Sep 25, 2016, 11:56:33 PM9/25/16
to web...@googlegroups.com
Hi,

Not sure if this is expected behavior but I can't figure out any reason why it is done like this.

Currently include tag messes up the indentation level. Only first line of included file is indented same level as parent file, other lines start from zero. I think indentation level should be preserved, so that indentation in include tag is added to every line in included file.

How to fix that problem?

Anthony

unread,
Sep 26, 2016, 7:17:24 AM9/26/16
to web2py-users

The {{include}} statement is intended to include exactly what is in the included file, starting at the point of the {{include}} -- if the text in the included file is not indented, then neither will it be indented once included. If you want it indented, then you must indent the text in the included file.

Also, note that if you are producing HTML, indenting is not necessary anyway.

Anthony

Marko Seppälä

unread,
Sep 26, 2016, 7:54:58 AM9/26/16
to web2py-users

Yes I'm producing HTML and I know it's not a big problem since it's not affecting to functionality. But still I prefer end result which looks clean and is indented in a proper manner. Indenting matters for the readability of HTML so I want it to be right.

I could get a right end result if I manually indent every other than first line in a included file so that it match to indentation level in file where it is included. But I think this is even worse since it mixes up my original source code. In my opinion, include statement should work so that it preserves the original indentation. So if include statement is indented for example three tabs, then every line in included file should be indented three tabs plus their indentation in original file. This way your source code and end result looks always right.

 Is there any quick fix how I could implement that? I haven't check web2py template engine yet.

- Marko

Anthony

unread,
Sep 26, 2016, 9:55:13 AM9/26/16
to web2py-users

The {{include}} statement is intended to include exactly what is in the included file, starting at the point of the {{include}} -- if the text in the included file is not indented, then neither will it be indented once included. If you want it indented, then you must indent the text in the included file.

Also, note that if you are producing HTML, indenting is not necessary anyway.

Anthony

Yes I'm producing HTML and I know it's not a big problem since it's not affecting to functionality. But still I prefer end result which looks clean and is indented in a proper manner. Indenting matters for the readability of HTML so I want it to be right.

Readability is most important in the files you edit (i.e., the view files). Why do you need the final HTML to be so readable? If it's for debugging, I suppose you could always pass the output through something like Beautiful Soup prettify() when in development.
 
I could get a right end result if I manually indent every other than first line

Or indent everything (including the first line) in the included file but don't indent the {{include}} in the extended view.
 
In my opinion, include statement should work so that it preserves the original indentation. So if include statement is indented for example three tabs, then every line in included file should be indented three tabs plus their indentation in original file.

That would not be good default behavior because we can't always assume leading space represents indentation. Indentation would need to be an option triggered by some kind of flag.
 
Is there any quick fix how I could implement that?

I don't think so. I would suggest using an alternative templating engine, but I don't think others (e.g., Django, Jinja2, Mako) provide this option either (solutions I've seen require you to specify the indentation level, somewhat defeating the purpose).

You could probably write a function to add the indentation, but it would have to be called dynamically on the included template, which would kill the speed benefit of compiled views.

Anthony

Marko Seppälä

unread,
Sep 26, 2016, 11:18:20 AM9/26/16
to web2py-users

It actually doesn't matter that much for me, I only work with web2py templates.  End result just looks little unprofessional since it's not formatted properly and I couldn't figure out any reason why not to do so. But I understand now your point. Django doesn't have this feature either.

I think this would be a nice to have feature in production to get cleaner and more professional looking end result. And human readable HTML helps also in development, especially if you're working with someone who is not familiar with web2py templates. Optional flag solution would be good which could be set if we want to preserve the indentation.

Anthony

unread,
Sep 26, 2016, 1:11:06 PM9/26/16
to web2py-users

It actually doesn't matter that much for me, I only work with web2py templates.  End result just looks little unprofessional since it's not formatted properly and I couldn't figure out any reason why not to do so.

"Unprofessional" to whom? Who's looking at the page source that you're trying to impress (that wouldn't understand the nature of template engines and the unimportance of indentation)?
 
I think this would be a nice to have feature in production to get cleaner and more professional looking end result.

Production is the one place you probably don't want this feature, as it would require a little extra processing and increase the payload.
 
And human readable HTML helps also in development, especially if you're working with someone who is not familiar with web2py templates.

Again, keep in mind that you can just indent the content in the included file. Any decent code editor will let you quickly indent multiple lines simultaneously.

Anthony

Marko Seppälä

unread,
Sep 26, 2016, 2:01:07 PM9/26/16
to web2py-users

Yes I know I can indent multiple lines simultaneously but it's still manual and unnecessary work, also messes up my source so not really an option. The whole point was that if there is no harm to indent everything properly by template engine, then why not to do it. But I understood your point why it's not implemented by default so basically it answers to my question.

Not sure how much more processing and payload is needed if this is implemented. If it's practically zero then I would do it but of course if it's significant then it's better to let it be like it's now.
Reply all
Reply to author
Forward
0 new messages