Mako template style question: remove whitespace before code block tags

1,666 views
Skip to first unread message

Christian Wutte

unread,
Feb 22, 2012, 2:48:03 PM2/22/12
to Mako Templates for Python
Hello all,
I would like to use the following style in template files:
% for e in elems:
% if flag:
<% .. some other python code .. %>\
.. template output ..
% endif
% endfor

Obviously the whitespace before "<%" appears in the output as well.
My idea with the above style was that the output and the code is
better visually separated.

Can I get rid of the whitespace?

% for e in elems:
%if flag:
<% .. some other python code .. %>\
.. template output ..
% endif
% endfor


Thanks

Christian Wutte

unread,
Feb 22, 2012, 2:51:31 PM2/22/12
to Mako Templates for Python
(too fast post because of tab reflex)

Last question was I have to stick with that way which doesn't look as
good:
% for e in elems:
% if flag:
<% .. some other python code .. %>\
.. template output ..
% endif
% endfor

Regards

binadam

unread,
Feb 23, 2012, 11:38:53 PM2/23/12
to Mako Templates for Python
Why do you need the backslash at the end of your python block?

Christian Wutte

unread,
Feb 24, 2012, 7:10:42 AM2/24/12
to Mako Templates for Python
The backslash omits the newline, which is also unwanted.

Somehow I could not find a specific Mako style recommendation but from
the docs I think the following would be suggested:
% for e in elems:
% if flag:
<%
.. some multiline
python code ..
%>\
.. template output ..
% endif
% endfor

In the above example the different indent of control lines' % and code
block tags is somehow irritating to me.

But rethinking the second post:
% for e in elems:
% if flag:
<%
.. some multiline
python code ..
%>\
.. template output ..
% endif
% endfor

At least in that example the additional indent after the % and tags
start leads to a nice look as they are just right seperated from the
actual code.
(I am working with templates which are quite nested (about to refactor
that) plus there are several lines of code blocks/lines throughout the
template files.)

Thanks for any thoughts on that.

Jeff Rimko

unread,
Nov 9, 2012, 12:06:07 PM11/9/12
to mako-d...@googlegroups.com
Realize this is an old post but I'm am encountering the same issue. One possible solution is to add a new set of tags to Mako that would ignore the included lines in the output. Maybe "<%- python code... %>" would be a possible format for the tags (note the dash in the opening tag). One nice side effect is that there would be no need for the trailing backslash after the closing tag since the entire line would be excluded from the output.

Michael Bayer

unread,
Nov 9, 2012, 6:14:20 PM11/9/12
to mako-d...@googlegroups.com
From what I can see this is just a new syntax that doesn't actually do anything not already possible, just moves the "\" to "<%-", is that it?   Concerned that this presents "more than one obvious way" of doing it.   The backslash is typically the one-stop-shop for ad-hoc newline removal, and then a filter that flattens whitespace would be the more "bulk" version of that.


--
You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mako-discuss/-/6y8eEj3LmBoJ.
To post to this group, send email to mako-d...@googlegroups.com.
To unsubscribe from this group, send email to mako-discuss...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mako-discuss?hl=en.

Bhavya V Hegde

unread,
Nov 19, 2012, 11:47:14 AM11/19/12
to mako-d...@googlegroups.com

B

--
Reply all
Reply to author
Forward
0 new messages