Python Flask Render Text from a variable e.g. render_template

I know the render_template bulb render_template . I must specify the name of the template file. But now I want to display the template string (i.e. the contents of the template). It makes sense. but now I don’t want to explain why. How can you simply visualize the template text?

+7
python flask jinja2
source share
4 answers

You can use render_template_string :

 >>> from flask import render_template_string >>> render_template_string('hello {{ what }}', what='world') 'hello world' 
+17
source share

you can use from_string

 template = "text {{ hello }}" print app.jinja_env.from_string(template).render(hello='Hello') >> text Hello 
+2
source share

In fact, you can directly call the jinja2 rendering function:

 jinja2.Template("I am {{ var }}").render(**paramaters) 

If you do not work with a flask, it is useful

0
source share

Taken from The easiest way to avoid HTML in Python .

 import cgi rendered = render_template('template.html') return cgi.escape(rendered) 
-one
source share

All Articles