Search code examples
pythondjangotemplatingpystache

Pystache without escaping (unescaped)


I'm using pystache to render the templates. I'm getting & in the output when I render context variables having &. How can get rid of & where I need & . Same thing is happening with django templating as well

>>> pystache.render('The URL {{URL}}', {'URL': 'http://google.com?a=3&b=3'})
u'The URL http://google.com?a=3&b=3'

Solution

  • To prevent escaping use triple braces {{{var}}}

    To prevent escaping, use triple braces, {{{URL}}} instead of double braces {{URL}}

    >>> pystache.render('The URL {{{URL}}}', {'URL': 'http://google.com?a=3&b=3'})
    u'The URL http://google.com?a=3&b=3'
    

    I've tested this on the most recent release as of today, version 0.5.4

    Mustache Documentation

    Since Pystache is a Mustache implementation in Python, you can use Mustache's documentation as pointers.

    All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache: {{{name}}}.

    source: https://mustache.github.io/mustache.5.html