2

I am trying to include a copyright symbol in the output of a view method in a Pyramid application. This is literally the method:

def get_cpyright(self):
    cpyright = ["My Super App © 2012"]
    if (datetime.datetime.now().year > 2012):
        cpyright.append(" - %d " % datetime.datetime.now().year)
    return " ".join(cpyright)

However, when it renders out to the application, the web-page reads:

My Super App © 2012

In straight web-dev, I could use ©or © I would get the the © character. This is not the case when rendering the string in the tuple.

How can I get the copyright symbol to show up? I see it on Pyramid's own docs, so I know it's possible.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Jon Mitten
  • 1,965
  • 4
  • 25
  • 53

3 Answers3

4

The reason is that your template variable is being escaped for an HTML context and © is escaped into ©.

If you're using chameleon templating you can use ${structure:variable} to avoid it being escaped when displaying (as you've already escaped it in your method). You can also wrap it in an object that has a __html__ method which returns the content. See In Pyramid, how do I return raw HTML from a view? for more information.

Community
  • 1
  • 1
MatsLindh
  • 49,529
  • 4
  • 53
  • 84
1

Why not just make the Python file unicode, make sure the output html is an appropriate encoding and write the copyright symbol like this?

def get_cpyright(self):
    cpyright = [u"My Super App © 2012"]
    if (datetime.datetime.now().year > 2012):
        cpyright.append(" - %d " % datetime.datetime.now().year)
    return " ".join(cpyright)
kojiro
  • 74,557
  • 19
  • 143
  • 201
  • 1
    @JonMitten If the python source file is not yours to change, doesn't that make it impossible to implement the fix in the answer you accepted? – kojiro Aug 08 '12 at 00:09
  • I should say, changing the encoding would require my whole team to consider the change. I applied the fix in-line, and it worked. We're on Python 2.7, so I assume ASCII, but for all I know, the encode type could already be UTF-8. – Jon Mitten Aug 08 '12 at 17:47
1

It's easier than you think to mess up your encoding in source code files. Try opening it in a bunch of different editors over time and you'll probably end up breaking the encoding sooner or later.

I would simply do this:

print unichr(169)

Editing your function I would replace your first line with this:

cpyright = ''.join(["My Super App ", unichr(169), " 2012"])
jesper
  • 227
  • 3
  • 9
  • 3
    I disagree; the `join` and extra syntax is worse. You can even keep the numeric value separated from the rest if that's your concern, as Python concatenates adjacent string literals in its lexer. `'My Super App ' u'\u00a9' ' 2012' == u'My Super App \u00a9 2012'` – ephemient Aug 07 '12 at 22:25
  • The only thing about that particular solution I'm really against is the hexadecimal representation. But that might boil down to a matter of personal taste. The adjacent string literal concatenation is nice, though :) – jesper Aug 07 '12 at 22:37
  • 3
    Nobody looks up Unicode characters by _decimal_ value ;-) (I do agree that it is a matter of taste.) – ephemient Aug 07 '12 at 22:45
  • I did this without the ''.join() and it works like a charm. Here's thew new format: `cpyright = ["My Super App " + unichr(169) +" 2012"]` – Jon Mitten Aug 07 '12 at 23:39