1

I have some formatted data in MongoDB, named i:

<p><strong>some string</strong></p>

But when I render it with flask and jinjia, like:

{% for i in example %}
    <div>{{ i }}</div>
{% endfor %}

The Browser show me:

<p><strong>some string</strong></p>

But I want to just get:

some string

I do it using ajax and put the formatted data in html, using jQuery html() method.

But how can I do it just in template rendering part?

2
  • It would really help to see your code here. A github url would suffice. Cheers! Commented Jan 13, 2015 at 14:18
  • 2
    Have you tried the safe filter? jinja.pocoo.org/docs/dev/templates/#safe Commented Jan 13, 2015 at 14:18

1 Answer 1

2

Jinja2 escapes html by default. To mark data as safe for printing as html, use the safe filter.

Either like this:

{{ myvariable|safe }}

Or turn escaping off for a block:

{% autoescape false %}
    <p>autoescaping is disabled here
    <p>{{ will_not_be_escaped }}
{% endautoescape %}

Flask depends on Jinja by default, but you can use a different template engine if you please, but Jinja still needs to be installed.

More information:

Sign up to request clarification or add additional context in comments.

2 Comments

It really helps, thank you. Before your answer, I thought the problem was caused by flask framework.
It's easy to confuse them, I added some links to my answer for future reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.