jinja icon indicating copy to clipboard operation
jinja copied to clipboard

Support Unicode literals in templates

Open eastside opened this issue 11 years ago • 9 comments

Sometimes, it makes sense to format a character in a string literal--

{{ "{0} is fantastic".format(user.full_name) }}

However, this will fail with a UnicodeEncodeError if user.full_name is a Unicode string with non-ASCII characters in it.

Jinja2 does not support unicode string literals, so one must work around this problem like so--

{{ "{0} is fantastic".decode("utf8").format(user.full_name) }}

This is icky. Could Jinja2 be changed in the future to support the Unicode literal? Our example code would look like this--

{{ u"{0} is fantastic".format(user.full_name) }}

This mirrors what you would likely do in a Python script, which I think is what Jinja2 is going for.

Aside: I think you might be wondering what the real use case is for this. Obviously, in the example I've used it would be easy to just write this:

{{ user.full_name }} is fantastic!

One example of where a unicode literal makes sense is encoding a querystring. For example, imagine we have a mailto link with some default body and subject text, which we want to format. This pattern makes sense:

{% set mailto_dict = {
    "body": "{0} is fantastic!".decode("utf8").format(user.full_name),
    "subject": "And this is some example text! Sincerely, {0}".decode("utf8").format(business.full_name),
} %}
<a href="mailto:?{{ mailto_dict|urlencode }}">Send an email</a>

But all those decode("utf8") are icky. It would be much nicer to use a Unicode literal.

eastside avatar Dec 17 '14 19:12 eastside

+1

razor-1 avatar Feb 05 '16 01:02 razor-1

Umm jinja is unicode based so if operations involving unicode strings fail that's a bug. Having two types of strings in jinja makes no sense.

ThiefMaster avatar Feb 05 '16 01:02 ThiefMaster

I think the problem here is that the format string itself consists only of ASCII-characters, and such literals are treated by Jinja as (non-Unicode) str instances. But calling .format with Unicode data then fails.

I can understand the rationale behind using str for pure-ASCII string literals, but it would be nice if there was a way to force string literals to Unicode (like Python's from future import unicode_literals). The syntax could be similar to that of the Autoescape extension:

{% unicodeliterals true %}
    {# All string literals are Unicode literals #}
    {% set foo = "I'm a unicode literal!" %}
{% endunicodeliterals %}

{% unicodeliterals false %}
    {# Standard behavior (type depends on content) #}
    {% set bar = "I'm a str literal!" %}
    {% set baz = "Ünicödë för thë wïn!" %}
{% endunicodeliterals %}

As a workaround you can use the format filter instead. However, it seems that it only supports the old (Python 2) string formatting syntax.

torfsen avatar Mar 22 '16 10:03 torfsen

@torfsen That sounds reasonable, especially if there is a global way to enable unicodeliterals so that everything is just treated that way.

razor-1 avatar Mar 22 '16 15:03 razor-1

+1

byoungb avatar Apr 15 '16 19:04 byoungb

This is actually a "bug" with .format which does not support string coercion on Python 2. We intentionally do not issue string literals as unicode literals on python 2 if they are in the ASCII range as APIs traditionally use coercion on it. I would file this as a bug against Python and not Jinja :)

When they introduced .format in Python 2 they applied Python 3 semantics on it. This does not work well with how Jinja is structured. I don't think I can do anything about this without breaking other code that requires bytestrings.

Traditionally datetime.strftime could not at all work with unicode strings for instance. Maybe we can slowly relax that requirement but there are still lots of apis that break when unicode data is passed on 2.x

mitsuhiko avatar Apr 15 '16 20:04 mitsuhiko

>>> '%s' % u'\N{SNOWMAN}'
u'\u2603'
>>> '{}'.format(u'\N{SNOWMAN}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2603' in position 0: ordinal not in range(128)

mitsuhiko avatar Apr 15 '16 20:04 mitsuhiko

So should this be closed as a "can't fix"? Or does a note need to be added to the docs somewhere about this?

As a workaround you can use the format filter instead. However, it seems that it only supports the old (Python 2) string formatting syntax.

FYI--the current plan (open to input) is to deprecate the format filter, see: https://github.com/pallets/jinja/issues/566

jeffwidman avatar Apr 16 '16 05:04 jeffwidman

@jeffwidman note that % does the same thing anyways.

mitsuhiko avatar Apr 20 '16 10:04 mitsuhiko