Is there a way to pass variables into Jinja2 parents?

后端 未结 3 818
北海茫月
北海茫月 2021-01-04 02:15

I\'m trying to pass some variables from the child page to the template. This is my python code:

    if self.request.url.find(\"&try\") == 1:
        isTr         


        
3条回答
  •  余生分开走
    2021-01-04 03:14

    The example on the Jinja2 Tips and Tricks page explains this perfectly, http://jinja.pocoo.org/docs/templates/#base-template. Essentially, if you have a base template

    **base.html**
    
        
             MegaCorp -{% block title %}{% endblock %}
        
        
            
    {% block content %}{% endblock %}

    and a child template

    **child.html**
    {% extends "base.html" %}
    {% block title %} Home page {% endblock %}
    {% block content %}
    ... stuff here
    {% endblock %}
    

    whatever python function calls render_template("child.html") will return the html page

    **Rendered Page**
    
        
             MegaCorp - Home 
        
        
            
    stuff here...

提交回复
热议问题