Is there a way to pass variables into Jinja2 parents?

后端 未结 3 788
北海茫月
北海茫月 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 02:59

    I do not understand your problem. When you pass variables to the context (as you do with trying) these variables will be available in the child and the parent. To pass title to the parent, you have to use inheritance, sometimes in combination with super : http://jinja.pocoo.org/docs/templates/#super-blocks

    See also this question: Overriding app engine template block inside an if

    0 讨论(0)
  • 2021-01-04 03:04

    I think that you are looking to highlight active menus in the base layout and you need something like this

    {% extends 'base.html' %}
    {% set active = "clients" %}
    

    then use can use "active" inside base.html

    0 讨论(0)
  • 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**
    <html>
        <head>
            <title> MegaCorp -{% block title %}{% endblock %}</title>
        </head>
        <body>
            <div id="content">{% block content %}{% endblock %}</div>
        </body>
    </html>
    

    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**
    <html>
        <head>
            <title> MegaCorp - Home </title>
        </head>
        <body>
            <div id="content">
                stuff here...
            </div>
        </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题