How do you display markdown value using jinja2?

LLaP

I process a string on the server using python markdown2 module.

marked_up = '    import sys\n    print "hello there"' 
marked_up = unicode(markdown2.markdown(marked_up, extras=["fenced-code-blocks"]))

Then, I pass the value through jinja2 to the client:

template_value = {'marked_up': marked_up}

template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_value))

In the index.html I try to display this marked value:

<div class="row marketing" id="my_row_mark">

   {{ marked_up }}

</div>

The problem is that the text is shown with the html attributes:

<pre><code>import sys print "hello there" </code></pre>

I would like to see only:

import sys print "hello there"

with the proper markdown applied by markdown2.

Lukas Graf

TL;DR:

Use the |safe filter to prevent your content from getting automatically escaped:

{{ marked_up|safe }}



Jinja2 has a configuration option named autoescape that determines if content in templates should be automatically HTML escaped or not.

By default (if you're using plain Jinja2) autoescaping is disabled. But if your using a framework that integrates Jinja2, autoescape may very well be enabled.

So when autoescaping is enabled, any content you pass into a template will be HTML escaped. Note in this example how content will be escaped, which leads to you seeing the HTML tags in the rendered HTML:

example.py

from jinja2 import Environment, FileSystemLoader

env = Environment(loader=FileSystemLoader('.'),
                  autoescape=True)              # <-- autoescaping enabled

template = env.get_template('index.html')
content = "<strong>bar</strong>"
print template.render(content=content)

index.html

<div>
    {{ content }}
</div>

Output:

<div>
    &lt;strong&gt;bar&lt;/strong&gt;
</div>

You can now prevent escaping for a particular variable by using the |safe filter:

<div>
    {{ content|safe }}
</div>

Output:

<div>
    <strong>bar</strong>
</div>

For more details, see the documentation on HTML escaping.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I fetch a value from a var file using Jinja2 into an JSON file in Ansible

How do you sort a list in Jinja2?

In Jinja2, how do you test if a variable is undefined?

How do you display a figure in matplotlib using PySide2?

How do you make lettered lists using markdown?

Using VSTS Rest API, how do you Update a Markdown widget?

How can you change the color of a jinja2 entry depending on some condition? (using flask)

How to display a bytes type image in HTML/Jinja2 template using FastAPI?

How to display uploaded image in HTML page using FastAPI & Jinja2?

How to get dict key value format using Jinja2 in ansible using debug or set fact module

How do I assign a jinja2 variable value to use later in template?

How to change parent style based on child's value using Jinja2/Python?

How to submit HTML form <input> value using FastAPI and Jinja2 Templates?

How do I use a global variable in Jinja2 using namespace class?

How do I view a session variable using Flask/Jinja2?

How do I append static items to a dict using Jinja2

How do I show link image by using Python Flask jinja2?

Flask with Jinja2 - Passing value from Flask to Javascript using Jinja2 statements

how do you create a subsection in R markdown

How do you center text in Gitlab markdown?

How do you embed an iframe into markdown

How do you display a Toast using Kotlin on Android?

How Do You Make a Responsive Grid Using - Display :Grid

How to display string representation of a context object in Jinja2?

store html checkbox value into list using flask, jinja2

How to display {% raw %} and {% endraw %} using markdown?

How do you control float formatting when using DataFrame.to_markdown in pandas?

How do you indent a bulleted list in a README file using GitHub flavored markdown?

How to get single value from jinja2 template?