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?
render_template
You can use render_template_string :
render_template_string
>>> from flask import render_template_string >>> render_template_string('hello {{ what }}', what='world') 'hello world'
you can use from_string
template = "text {{ hello }}" print app.jinja_env.from_string(template).render(hello='Hello') >> text Hello
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
Taken from The easiest way to avoid HTML in Python .
import cgi rendered = render_template('template.html') return cgi.escape(rendered)