user592419
user592419

Reputation: 5223

How do I dynamically use include in a Flask Template?

I have a flask backend sending data to the client. Some JS on the client side then utilizes that data to construct a page that includes one row of data and another area below the row that has a d3 display with clickable nodes.

The nodes have different properties corresponding to different models in the backend and different html templates. Given a type, when clicked, a different html template is presented in the row above.

How do I set the template from the JS? I tried doing something like:

$("#div_id").innerHTML = "{% set obj = " + obj + "%}{% include '" + obj_template_file + "' %}"

That (perhaps of course) didn't work. After taking care of escaping, it just displays that string. I thought of dirty ways of doing this like using hide and show liberally but that doesn't seem wise.

Upvotes: 2

Views: 1244

Answers (1)

CESCO
CESCO

Reputation: 7758

ALthough is not clear what you mean by "sending data to the client". You can pass the base_template argument from your routes like:

@auth.route('/<path:path>', methods=['GET', 'POST'])
def password_reset_request():
    if path ==2:
         base_template = "jay.html"
    if path ==3:
         base_template = "blue.html"
    ...
    return render_template('auth/reset_password.html', base_template=base_template)

and on the template:

{% extends base_template %}

THen from your javascript frontend you just need to GET the URL.

Upvotes: 2

Related Questions