Wrapping Mako-generated text to a fixed width

148 views
Skip to first unread message

jgarbers

unread,
Feb 16, 2012, 6:29:10 PM2/16/12
to Mako Templates for Python
I'm using Mako templates to generate emails with lots of interpolated
variables. I'd like to be able to use Python's textwrap module to
force some of the paragraphs in the email wrap to a specified column
width.

I could "pre-wrap" the text before interpolating it into my template,
but I'd prefer to have the text for those paragraphs simply live in
the template.

Can someone suggest a convenient approach to tag or mark a paragraph
in a template so it can have a function -- in this case, presumably,
textwrap.wrap() -- applied to it?

Thanks!

Michael Bayer

unread,
Feb 16, 2012, 6:59:57 PM2/16/12
to mako-d...@googlegroups.com

the <%block> tag was made for this sort of thing:

from mako.template import Template

t = Template("""
<%block filter="wrap">
alsfalskmf alsdkfm aslkf asdlkf aslkfmaslfma ds skjfna ksjfn adkjfn adskjfn
</%block>
""")

import textwrap
def wrap(text):
return "\n".join(textwrap.wrap(text, 40)).strip()

print t.render(wrap = wrap)

>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
> 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.
>

Reply all
Reply to author
Forward
0 new messages