Python pyramid and chameleon escapes html template language

I can not understand the chameleon tags. I am a django user, but decided to introduce my CompSci companions and myself to Pyramid, since I, although lighter, is easier to learn.

The $ {} tag is currently escaping any html tags that I am trying to output through it. In django, there is some way to indicate that a variable is "safe" and does not need to be escaped.

How can I do the same in Pyramid / Chameleon?

+3
source share
2 answers

Chameleon is based on Zope Page Templates , so if you don't have enough documentation for Chameleon, you can check out the zpt docs.

, . tal: replace tal: content, . structure , , , , . :

s = '''
<html>
    <head>
    </head>
    <body>
        <div tal:content="structure t">
        </div>
    </body>
</html>
'''

from chameleon import PageTemplate

pt = PageTemplate(s)

print pt(t='<p>Hi!</p>')

tal: replace tal: content, , Chameleon ( , __html__, ). , "", :

a = '''
<html>
    <head>
    </head>
    <body>
        <div>
            ${t}
        </div>
    </body>
</html>
'''

from chameleon import PageTemplate

pt = PageTemplate(a)

class Literal(object):
    def __init__(self, s):
        self.s =s

    def __html__(self):
        return self.s

print pt(t=Literal('<p>Hi!</p>'))
+9

${structure: markup}.

+14

All Articles