chirinosky
chirinosky

Reputation: 4538

Jinja2 - Process variable from Flask view

I have a Flask application that allows users to define templates for certain sections within a main Jinja2 template. Is it possible to have Jinja process a variable from a string? For example, application view passes the below variable:

report.summary='<p>This is a report for {{ user.first_name }}.</p>'

The relevant portion from the Jinja template is:

<h1>Summary</h1>
{{ report.summary }}

The rendered output is:

Summary

This is a report for {{ user.first_name }}.

Can Jinja process {{ user.first_name }}, or am I forced to do the variable substitution myself from flask?

Upvotes: 1

Views: 485

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191701

I think you can use render_template_string.

report.summary=render_template_string(r'<p>This is a report for {{ user.first_name }}.</p>', user) 

Documentation: http://flask.pocoo.org/docs/0.10/api/#flask.render_template_string


Otherwise, why have HTML in your class fields when you can just pass both the user and the report to the template?

Upvotes: 2

Related Questions