Mako and whitespace

491 views
Skip to first unread message

Matt H

unread,
Oct 5, 2010, 10:17:25 PM10/5/10
to pylons-discuss
Hi guys.

How would I go about removing whitespace from the html output of my
mako templates? I see that in the /data/templates directory the
templates are cached with a ton of whitespace, so how would I execute
a function on the output before it gets cached by Mako?

Deron Meranda

unread,
Oct 7, 2010, 12:55:22 AM10/7/10
to pylons-...@googlegroups.com

You may get better responses on the Mako list rather than Pylons,
but without further details I suspect there are a couple things you
may want to do.

First, when you write your templates be aware of where you may
be inadvertently introducing whitespace. Make use of the end-of-line
backslash syntax to suppress extra line breaks, especially when
ending a Mako block. Rather than

Some text
<%
some python code
%>
more text.

Do something like this:

Some text\
<%
some python code
%>\
more text.

Also don't insert unnecessary blank lines if you don't need them,
instead just insert a blank Mako comment line: ##


Secondly, if you want to remove some whitespace by passing the
output through a function prior to caching you may want to use
Mako's filter mechanism. It already has a built-in filter to strip
whitespace at the beginning and end. If you want something more
sophisticated you can provide your own filtering function. Read up
on Mako's filters:

http://www.makotemplates.org/docs/filtering.html

If you have a more specific example that may help to answer
your question.
--
Deron Meranda
http://deron.meranda.us/

Reply all
Reply to author
Forward
0 new messages