You can define a function to load the text file and render it in the template:
import jinja2
def include_file(name):
return jinja2.Markup(loader.get_source(env, name)[0])
loader = jinja2.PackageLoader(__name__, 'templates')
env = jinja2.Environment(loader=loader)
env.globals['include_file'] = include_file
def render():
return env.get_template('page.html').render()
if __name__ == '__main__':
print render()
In the template, call it like this:
{{ include_file('file.txt') }}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…