2017-08-21 96 views
-1

我在建造一個聊天機器人。有幾個像login.html,messages.html,transaction.html等子模板。我想動態地在base.html中追加這些模板。我在所有這些模板中擴展base.html。我的問題是一次只呈現一個模板。有沒有解決方案可以一個接一個地追加這些模板?我使用了{%include%},但它是一種靜態方法。我需要動態的。燒瓶,jinja2 - 動態追加模板

printer.py樣子 -

@app.route('/respond', methods=['GET','POST']) 
def respond_def(): 
    message = request.form['message_input'] 
    if message == "l": 
     return render_template('printer/login.html') 
    elif message == "t": 
     return render_template('printer/transactionID.html') 

base.html文件看起來像 -

//some code here 
<li> 
    {% block template %}{% endblock %} 
</li> 
//some code here 

message.html樣子 -

{% extends "base.html" %} 
{% block template %} 
<div> Message template called </div> 
{% endblock %} 

回答

0

我解決它。 我在printer.py中創建了一個模板列表,然後在用戶詢問它時在base.html中添加這些模板。

printer.py

dictionary = [] 
// append name of template in this whenever needed. 
return render_template('printer/base.html', dictionary=dictionary) 

base.html文件

{% for d in dicts %} 
{% set template = 'printer/' + d + '.html' %} 
// can add conditions for different templates 
{% include template %} 
{% endfor %}