Search code examples
pythonjinja2

How to disable Jinja2 for sections of template with {}?


I added a facebook button to my page by copying/pasting the code they supply on their website.

It looks like this:

"http://www.facebook.com/dialog/feed?app_id={{fbapp_id}}&link={{link_url}}&message={{share_message|urlencode}}&display=popup&redirect_uri={{link_url}}  

As you can see, it's got the {} in there that Jinja looks for. However, being that I don't want any of the above code replaced with anything, is there something I can add into my template which tells Jinja to ignore everything between the delimiters?

Python Handler:

class MainHandler(webapp2.RequestHandler):
    def get(self):
        template = JINJA_ENV.get_template('index.html')
        self.response.write(template.render(None))

Solution

  • You can usually find that information in the documentation, under "Escaping" or similar. In this case, you can either output the delimiter with a variable expression:

    {{ '{{' }}
    

    Or you can use the raw block, for longer stretches of code:

    {% raw %}
        <ul>
        {% for item in seq %}
            <li>{{ item }}</li>
        {% endfor %}
        </ul>
    {% endraw %}