user3605780 user3605780 - 1 year ago 344
Python Question

jinja2 load template from string: TypeError: no loader for this environment specified

I'm using Jinja2 in Flask. I want to render a template from a string. I tried the following 2 methods:

rtemplate = jinja2.Environment().from_string(myString)
data = rtemplate.render(**data)


rtemplate = jinja2.Template(myString)
data = rtemplate.render(**data)

However both methods return:

TypeError: no loader for this environment specified

I checked the manual and this url:

However nowhere is specified to select a loader when using a string.

Answer Source

You can provide loader in Environment from that list

from jinja2 import Environment, BaseLoader

rtemplate = Environment(loader=BaseLoader).from_string(myString)
data = rtemplate.render(**data)

Edit: The problem was with myString, it has {% include 'test.html' %} and Jinja2 has no idea where to get template from.