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
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
You are right. I added my hints to
http://docs.pythonweb.org/display/pylonscookbook/Using+Mako+templating+language
Christoph
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).
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.
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
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
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
Do you mean "encoding='utf-8'" or "convert_unicode=True" as documented
in http://www.sqlalchemy.org/docs/dbengine.html#dbengine_options ?
Christpoh
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
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>