Using Mako templates

20 views
Skip to first unread message

voltron

unread,
May 4, 2007, 3:43:47 PM5/4/07
to pylons-discuss
Hi,

I´ll like to use only Mako templates in my projects, apart from
stating this in the middleware.py, do I have to change these lines in
the einviroment.py:

# The following template options are passed to your template engines
tmpl_options = {}
tmpl_options['myghty.log_errors'] = True
tmpl_options['myghty.escapes'] = dict(l=webhelpers.auto_link,
s=webhelpers.simple_format)


Thanks

Christoph Haas

unread,
May 5, 2007, 6:08:39 AM5/5/07
to pylons-...@googlegroups.com

You can just comment them out. But I suggest you use:

tmpl_options = {}
tmpl_options['mako.input_encoding'] = 'UTF-8'
tmpl_options['mako.output_encoding'] = 'UTF-8'
tmpl_options['mako.default_filters'] = ['decode.utf8']

Otherwise Mako was bitchy about my UTF-8 encoded templates.

Christoph

voltron

unread,
May 5, 2007, 1:25:31 PM5/5/07
to pylons-discuss
Thank you very much for the tip Christoph. Hmm things like that should
be documented somewhere

Christoph Haas

unread,
May 5, 2007, 2:13:05 PM5/5/07
to pylons-...@googlegroups.com
On Sat, May 05, 2007 at 10:25:31AM -0700, voltron wrote:
> Thank you very much for the tip Christoph. Hmm things like that should
> be documented somewhere

You are right. I added my hints to
http://docs.pythonweb.org/display/pylonscookbook/Using+Mako+templating+language

Christoph

KyleJ

unread,
May 9, 2007, 2:18:12 PM5/9/07
to pylons-discuss
On May 5, 3:08 am, Christoph Haas <e...@christoph-haas.de> wrote:
> tmpl_options['mako.default_filters'] = ['decode.utf8']

You may wish to not use that option.

>From the Mako docs ( http://www.makotemplates.org/docs/unicode.html#unicode_handling
):
"Note that the built-in decode object is slower than the unicode
function, since unlike unicode its not a Python builtin, and it also
checks the type of the incoming data to determine if string conversion
is needed first."

If you comment that out and you start getting unicode errors, it's
likely because your strings aren't being converted to unicode objects.

You can either decode them yourself (one variable at a time
via .decode('utf-8') ), or you can have request.params output to
unicode (see http://pylonshq.com/project/pylonshq/browser/Pylons/trunk/docs/internationalization.txt#L516
) along with SQLAlchemy (use_unicode=True in either create_engine or
make_session).

Max Ischenko

unread,
May 10, 2007, 5:37:12 AM5/10/07
to pylons-...@googlegroups.com
On 5/9/07, KyleJ <osm...@gmail.com> wrote:

On May 5, 3:08 am, Christoph Haas <e...@christoph-haas.de> wrote:
> tmpl_options['mako.default_filters'] = ['decode.utf8']

You may wish to not use that option.

Second this.

I have  *no* tmpl_options for Mako and it just works. Passing utf8-encoded strings to Mako template violates the basic i18n rule of "use Unicode thoughtout your code and convert on I/O".

I'd ask to update the cookbook doc to reflect this.

Max.


Philip Jenvey

unread,
May 10, 2007, 4:47:13 PM5/10/07
to pylons-...@googlegroups.com

One thing I see mentioned a lot for both mako and myghty is the need
to set:

tmpl_options['engine.output_encoding'] = 'utf-8'

This is totally unnecessary in Pylons, as Pylons automatically sets
this value for mako and myghty to Config's response_settings
['charset'] value (which defaults to utf-8). That variable and
response_settings['content_type'] (defaulting to text/html) dictate
what's set as the outgoing Content-Type header, i.e.:

Content-Type: text/html; charset=utf-8

So if you're intending to send outgoing utf-8 data, the idea is
Pylons automatically sets your templating engine to output to that
encoding.

It doesn't do this for every templating engine though (I think it
does it for kid).

--
Philip Jenvey


Christoph Haas

unread,
May 10, 2007, 5:16:23 PM5/10/07
to pylons-...@googlegroups.com
On Wed, May 09, 2007 at 11:18:12AM -0700, KyleJ wrote:
>
> On May 5, 3:08 am, Christoph Haas <e...@christoph-haas.de> wrote:
> > tmpl_options['mako.default_filters'] = ['decode.utf8']
>
> You may wish to not use that option.
>
> >From the Mako docs ( http://www.makotemplates.org/docs/unicode.html#unicode_handling
> ):
> "Note that the built-in decode object is slower than the unicode
> function, since unlike unicode its not a Python builtin, and it also
> checks the type of the incoming data to determine if string conversion
> is needed first."
>
> If you comment that out and you start getting unicode errors, it's
> likely because your strings aren't being converted to unicode objects.

I had trouble indeed. But I'm willing to come back if that's happening
again.

> You can either decode them yourself (one variable at a time
> via .decode('utf-8') ), or you can have request.params output to
> unicode (see http://pylonshq.com/project/pylonshq/browser/Pylons/trunk/docs/internationalization.txt#L516
> ) along with SQLAlchemy (use_unicode=True in either create_engine or
> make_session).

Where (in a standard Pylons project) would I set use_unicode=True? I
just found references to "create_engine" in the websetup.py which is
obviously not the right place. And my models/__init__.py just has the
line

meta = DynamicMetaData()

Christoph

Shannon -jj Behrens

unread,
May 10, 2007, 5:37:13 PM5/10/07
to pylons-...@googlegroups.com

I fought long and hard with this problem. Back then, it turned out
that letting SQLAlchemy do the encoding with use_unicode=True wouldn't
work because of a bug in MySQLdb. However, it looks like that bug has
been fixed in a recent release of MySQLdb.

These days, I bet you can just add ?use_unicode=1 to the end of your
db uri, and it'll work.

Best Regards,
-jj

--
http://jjinux.blogspot.com/

Christoph Haas

unread,
May 10, 2007, 5:47:23 PM5/10/07
to pylons-...@googlegroups.com

Do you mean "encoding='utf-8'" or "convert_unicode=True" as documented
in http://www.sqlalchemy.org/docs/dbengine.html#dbengine_options ?

Christpoh

Shannon -jj Behrens

unread,
May 10, 2007, 5:52:46 PM5/10/07
to pylons-...@googlegroups.com

I bet encoding='utf-8' is the default anyway. I think
convert_unicode=True is what you want in the uri. The trick is to pay
attention to both what the database is storing and what MySQLdb is
using. There's three layers to worry about: SQLAlchemy, MySQLdb, and
MySQL (and pay attention to the default encoding, the default database
encoding, the default table encoding, and the field encoding...ugh).

Best of Luck,
-jj

--
http://jjinux.blogspot.com/

Mike Orr

unread,
May 11, 2007, 10:33:45 AM5/11/07
to pylons-...@googlegroups.com

No, use_unicode=1 is a MySQLdb option. I've been using it
successfully. MySQL is kind of a hassle to get Unicode working
reliably.

--
Mike Orr <slugg...@gmail.com>

Reply all
Reply to author
Forward
0 new messages